Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace calderawp\CalderaFormsQuery\Tests\Integration\Select;
use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase;
use calderawp\CalderaFormsQuery\Tests\Traits\HasFactories;
use calderawp\CalderaFormsQuery\Tests\Traits\UsersMockFormAsDBForm;
class EntryTest extends IntegrationTestCase
{
/** @inheritdoc */
protected $mock_form_id;
/** @inheritdoc */
protected $mock_form;
/** @inheritdoc */
/**
* Test query by form ID
*
* @covers Entry::queryByFormsId()
*/
public function testQueryByFormsId()
{
$entryGenerator = $this->entryGeneratorFactory();
$entryGenerator->queryByFormsId($this->mock_form_id);
$sql = $entryGenerator->getPreparedSql();
//No entries -> No results
$this->assertSame([], $this->queryWithWPDB( $sql) );
//One entry -> One result, with the right form ID.
$this->createEntryWithMockForm();
$results = $this->queryWithWPDB( $sql);
$this->assertTrue( ! empty( $this->queryWithWPDB( $sql) ) );
$this->assertSame( 1, count($results));
$this->assertSame( $results[0]->form_id, $this->mock_form_id );
//Two entries -> Two result, with the right form ID.
$this->createEntryWithMockForm();
$results = $this->queryWithWPDB( $sql);
$this->assertTrue( ! empty( $this->queryWithWPDB( $sql) ) );
$this->assertSame( 2, count($results));
$this->assertSame( $results[0]->form_id, $this->mock_form_id );
$this->assertSame( $results[1]->form_id, $this->mock_form_id );
}
/**
* Test query by entry ID
*
* @covers Entry::queryByEntryId()
*/
public function testQueryByEntryId()
{
$entry = $this->createEntryWithMockForm();
$entryGenerator = $this->entryGeneratorFactory();
//No results for a non-existent entry
$entryGenerator->queryByEntryId(42);
$sql = $entryGenerator->getPreparedSql();
$this->assertSame( [], $this->queryWithWPDB($sql));
//One entry: one result with the correct ID
$entryGenerator = $this->entryGeneratorFactory();
$entryGenerator->queryByEntryId($entry['id']);
$sql = $entryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $results ));
$this->assertSame( 1, count( $results ) );
$this->assertEquals( $results[0]->id, $entry['id']);
//Two more entries: one result for original entry ID
$this->createEntryWithMockForm();
$this->createEntryWithMockForm();
$entryGenerator = $this->entryGeneratorFactory();
$entryGenerator->queryByEntryId($entry['id']);
$sql = $entryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $results ));
$this->assertSame( 1, count( $results ) );
}
/**
* Test query by user ID
*
* @covers Entry::queryByUserId()
*/
/**
* Test query by user ID
*
* @covers Entry::queryByUserId()
*/
public function testQueryByUserId()
{
//Create an entry for without a user
$this->deleteAllEntriesForMockForm();
$entryDetailsNotLoggedIn = $this->create_entry($this->mock_form);
//Create an entry for a known user.
$this->factory()->user->create();
$userId = $this->factory()->user->create();
wp_set_current_user( $userId );
$this->assertEquals( $userId, get_current_user_id() );
$entryDetailsLoggedIn = $this->create_entry($this->mock_form);
//Make sure there is one entry with with this user
$entryGeneratorLoggedIn = $this->entryGeneratorFactory();
$entryGeneratorLoggedIn->queryByEntryId($entryDetailsLoggedIn['id']);
$sql= $entryGeneratorLoggedIn->getPreparedSql();
$resultsByEntryId = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $resultsByEntryId ));
$this->assertSame( 1, count( $resultsByEntryId ) );
$this->assertEquals( $userId, $resultsByEntryId[0]->user_id );
//Test that query by User ID gets the right entry
$entryGeneratorLoggedIn = $this->entryGeneratorFactory();
$entryGeneratorLoggedIn->queryByUserId($userId);
$sql= $entryGeneratorLoggedIn->getPreparedSql();
$resultsByUserId = $this->queryWithWPDB($sql);
$this->assertEqualSets((array)$resultsByEntryId[0], (array)$resultsByUserId[0]);
//Test that non-logged in user is tracked in DB as 0, not some actual user ID and we can select that way
$entryGeneratorNotLoggedIn = $this->entryGeneratorFactory();
$entryGeneratorNotLoggedIn->queryByEntryId($entryDetailsNotLoggedIn['id'] );
$sql= $entryGeneratorLoggedIn->getPreparedSql();
$resultsByEntryId = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $resultsByEntryId ));
$this->assertSame( 1, count( $resultsByEntryId ) );
$this->assertEquals( $userId, $resultsByEntryId[0]->user_id );
}
}