Newer
Older
<?php
namespace calderawp\CalderaFormsQuery\Tests\Integration\Features;
use calderawp\CalderaFormsQuery\CreatesSelectQueries;
use calderawp\CalderaFormsQuery\Delete\DeleteQueryBuilder;
use calderawp\CalderaFormsQuery\Features\FeatureContainer;
use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase;
use calderawp\CalderaFormsQuery\Tests\Traits\CanCreateEntryWithEmailField;
class FeatureHelperMethodsTest extends IntegrationTestCase
{
use CanCreateEntryWithEmailField;
/**
*
* @covers FeatureContainer::selectByUserId()
* @covers FeatureContainer::collectResults()
* @covers FeatureContainer::collectEntryValues()
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
*/
public function testByUserId()
{
$container = $this->containerFactory();
//Create an entry for a known user.
$email = 'nom@noms.noms';
$userId = $this->factory()->user->create(
[ 'user_email' => $email ]
);
wp_set_current_user( $userId );
$entryId = $this->createEntryWithEmail( $email );
$results = $container->selectByUserId( $userId );
$this->assertEquals( $entryId, $results[0]['entry']->id);
$this->assertEquals( $entryId, $results[0]['entry']->id);
$found = false;
foreach ( $results[0]['values'] as $entryValue )
{
if( $entryValue->slug === $this->getEmailFieldSlug() ){
$this->assertSame( $email, $entryValue->value );
$found = true;
}
}
$this->assertTrue( $found );
}
/**
* Test selecting by a field value such as an email
*
* @covers FeatureContainer::selectByFieldValue()
*/
public function testByFieldValue()
{
$container = $this->containerFactory();
//Create one entry for unknown user
$this->createEntryWithEmail( rand(). 'email.com' );
//Create two entries for a known user.
$email = 'nom@noms.noms';
$userId = $this->factory()->user->create(
[ 'user_email' => $email ]
);
wp_set_current_user( $userId );
$this->createEntryWithEmail( $email );
$this->createEntryWithEmail( $email );
$results = $container->selectByFieldValue(
$this->getEmailFieldSlug(),
$email
);
$this->assertSame(2, count($results));
$this->assertSame( $email,$results[0]['values'][1]->value );
$this->assertSame( $email,$results[1]['values'][1]->value );
}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
*
* @covers FeatureContainer::deleteByEntryIds()
* @covers FeatureContainer::delete()
*/
public function testDeleteByIds()
{
$container = $this->containerFactory();
//Create three entries
$entryIdOne = $this->createEntryWithMockFormAndGetEntryId();
$entryIdTwo = $this->createEntryWithMockFormAndGetEntryId();
$entryIdThree = $this->createEntryWithMockFormAndGetEntryId();
//Delete entry one and three
$container
->deleteByEntryIds([$entryIdOne,$entryIdThree]);
//No Entry results for entry One
$entryQueryGenerator = $this->entryGeneratorFactory();
$entryQueryGenerator->queryByEntryId($entryIdOne);
$sql = $entryQueryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertSame( 0, count( $results ) );
//No Entry Value results for entry One
$entryValuesQueryGenerator = $this->entryValuesGeneratorFactory();
$entryValuesQueryGenerator->queryByEntryId($entryIdOne);
$sql = $entryValuesQueryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertSame( 0, count( $results ) );
//Results for entry Two
$entryValuesQueryGenerator = $this->entryValuesGeneratorFactory();
$entryValuesQueryGenerator->queryByEntryId($entryIdTwo);
$sql = $entryValuesQueryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertTrue( 0 < count( $results ) );
}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Test deleting all entries for a specific user
*
* @covers FeatureContainer::deleteByUserId()
*/
public function testDeleteByUserId()
{
$container = $this->containerFactory();
//Create one entry for unknown user
$this->createEntryWithEmail( rand(). 'email.com' );
//Create two entries for a known user.
$email = 'nom@noms.noms';
$userId = $this->factory()->user->create(
[ 'user_email' => $email ]
);
wp_set_current_user( $userId );
$this->createEntryWithEmail( $email );
$this->createEntryWithEmail( $email );
//Delete messages for known user
$container->deleteByUserId($userId);
//Expect no entry results when querying by known user
$results = $container->selectByUserId($userId);
$this->assertSame(0, count($results));
//Expect no entry value results when querying by known user
$results = $container->selectByFieldValue(
$this->getEmailFieldSlug(),
$email
);
$this->assertSame(0, count($results));
}