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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
<?php declare(strict_types=1);
// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
class ConcertsTableTest extends WP_UnitTestCase
{
const VENUES = [
[ "Rockefeller Music Hall", "Oslo" ],
[ "Sentrum Scene", "Oslo" ],
[ "Revolver", "Oslo" ],
[ "Meieriet", "Sogndal" ],
];
const CONCERTS = [
[ "Concert at Rockefeller #", 0, 2 ],
[ "Concert at Sentrum Scene #", 1, 4 ],
[ "Concert at Revolver #", 2, 5 ],
[ "Concert at Meieriet #", 3, 5 ],
];
private static $concerts = [];
/* This function runs _once_ before all the test cases.
*
* Use it to set up a common state that all test cases can
* use
*/
static function wpSetUpBeforeClass() : void
{
$created_venues = [];
foreach (self::VENUES as $venue) {
$created_venues[] = GiglogAdmin_Venue::find_or_create($venue[0], $venue[1]);
}
$today = new DateTime();
foreach (self::CONCERTS as $concert) {
for ($i = 0; $i < $concert[2]; $i++) {
if ($concert[2] > 1) {
$concert_name = $concert[0] . ($i + 1);
}
else {
$concert_name = $concert[0];
}
$offset = rand(1, 13);
$concert_date = $today->modify( "+$offset days" );
self::$concerts[] = GiglogAdmin_Concert::create(
$concert_name,
$created_venues[$concert[1]]->id(),
$concert_date->format( 'Y-m-d' ),
"https://example.com/tickets/42",
"https://example.com/events/93");
}
}
}
/* This function runs _once_ after all the test cases in this class.
*
* It is needed to clean up changes in the database that we don't want
* to disturb any other tests.
*/
static function wpTearDownAfterClass() : void
{
global $wpdb;
$tables = [
"{$wpdb->prefix}giglogadmin_concerts",
"{$wpdb->prefix}giglogadmin_venues",
];
foreach( $tables as $table ) {
$wpdb->query("DELETE FROM {$table}");
}
}
function testShowAllControlsToAdminOnAdminPage() {
global $current_screen;
global $current_user;
$current_user = $this->factory()->user->create_and_get(['role' => 'administrator']);
$oldscreen = $current_screen;
$current_screen = WP_Screen::get( 'admin_init' );
$c = new GiglogAdmin_ConcertsTable();
$html = $c->render();
$current_screen = $oldscreen;
$doc = DOMDocument::loadHTML($html);
$forms = $doc->getElementsByTagName('form');
$assignit_count = 0;
$adminactions_count = 0;
foreach ($forms as $form) {
$cls = $form->attributes->getNamedItem('class')->nodeValue;
if ($cls == 'assign_concert' || $cls == 'unassign_concert') {
$assignit_count++;
}
if ($cls == 'adminactions') {
$adminactions_count++;
}
}
$this->assertEquals(60, $assignit_count); // four for each gig
$this->assertEquals(15, $adminactions_count); // once for each gig
}
function testDontShowAdminOnlyControlsToNonAdminsOnAdminPage() {
global $current_screen;
global $current_user;
$current_user = $this->factory()->user->create_and_get(['role' => 'editor']);
$oldscreen = $current_screen;
$current_screen = WP_Screen::get( 'admin_init' );
$c = new GiglogAdmin_ConcertsTable();
$html = $c->render();
$current_screen = $oldscreen;
$doc = DOMDocument::loadHTML($html);
$forms = $doc->getElementsByTagName('form');
$assignit_count = 0;
$adminactions_count = 0;
foreach ($forms as $form) {
$cls = $form->attributes->getNamedItem('class')->nodeValue;
if ($cls == 'assign_concert' || $cls == 'unassign_concert') {
$assignit_count++;
}
if ($cls == 'adminactions') {
$adminactions_count++;
}
}
$this->assertEquals(60, $assignit_count); // four for each gig
$this->assertEquals(0, $adminactions_count); // once for each gig
}
function testDontShowAnyControlsIfNotOnAdminPage() {
global $current_user;
// "log in" as administrator to make sure no admin side stuff is
// rendered on the public site, even if we're a high privilege user.
$current_user = $this->factory()->user->create_and_get(['role' => 'administrator']);
$c = new GiglogAdmin_ConcertsTable();
$html = $c->render();
$doc = DOMDocument::loadHTML($html);
$forms = $doc->getElementsByTagName('form');
$assignit_count = 0;
$adminactions_count = 0;
foreach ($forms as $form) {
$cls = $form->attributes->getNamedItem('class')->nodeValue;
if ($cls == 'assign_concert' || $cls == 'unassign_concert') {
$assignit_count++;
}
if ($cls == 'adminactions') {
$adminactions_count++;
}
}
$this->assertEquals(0, $assignit_count); // four for each gig
$this->assertEquals(0, $adminactions_count); // once for each gig
}
}
|