This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
culturefeed_my_program.test
320 lines (275 loc) · 9.08 KB
/
culturefeed_my_program.test
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* @file
* Tests for culturefeed_my_program.
*/
/**
* Defines a base class for testing the culturefeed_my_program module.
*/
class CulturefeedMyProgramWebTestCase extends DrupalWebTestCase {
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp(['culturefeed_my_program']);
}
}
/**
* Test CulturefeedMyProgramItem class.
*/
class CulturefeedMyProgramItemTestCase extends CulturefeedMyProgramWebTestCase {
/**
* Info.
*/
public static function getInfo() {
return [
'name' => 'Test CulturefeedMyProgramItem',
'description' => 'Test CulturefeedMyProgramItem class.',
'group' => 'CulturefeedMyProgram',
];
}
/**
* Test item create.
*/
public function testItemCreate() {
$values = [
'uid' => 666,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
];
$item = new CulturefeedMyProgramItem($values);
$this->assertEqual($values['uid'], $item->getUid(), 'Uid is correctly set.');
$this->assertEqual($values['item_id'], $item->getItemId(), 'Item ID is correctly set.');
$this->assertEqual($values['end_date'], $item->getEndDate(), 'End date is correclty set.');
$this->assertEqual($values['last_updated'], $item->getLastUpdated(), 'Last updated date is correctly set.');
}
/**
* Test item save and load.
*/
public function testItemSave() {
$values = [
'uid' => 666,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
];
$item = new CulturefeedMyProgramItem($values);
$item->save();
$db_item = CulturefeedMyProgramItem::load($values['uid'], $values['item_id']);
$this->assertEqual($values['uid'], $db_item->getUid(), 'Uid is correctly saved.');
$this->assertEqual($values['item_id'], $db_item->getItemId(), 'Item ID is correctly saved.');
$this->assertEqual($values['end_date'], $db_item->getEndDate(), 'End date is correclty saved.');
$this->assertEqual($values['last_updated'], $db_item->getLastUpdated(), 'Last updated date is correctly saved.');
}
/**
* Test item delete.
*/
public function testItemDelete() {
$values = [
'uid' => 666,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
];
$item = new CulturefeedMyProgramItem($values);
$item->save();
$item->delete();
$this->assertNull(CulturefeedMyProgramItem::load($values['uid'], $values['item_id']));
}
}
/**
* Test CulturefeedMyProgram class.
*/
class CulturefeedMyProgramTestCase extends CulturefeedMyProgramWebTestCase {
/**
* Info.
*/
public static function getInfo() {
return [
'name' => 'Test CulturefeedMyProgram',
'description' => 'Test CulturefeedMyProgram class',
'group' => 'CulturefeedMyProgram',
];
}
/**
* Test item save and load.
*/
public function testGetItems() {
$user = new stdClass();
$user->uid = 666;
$values = [
[
'uid' => $user->uid,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
],
[
'uid' => $user->uid,
'item_id' => '54ce03c7-4c89-4867-bf66-1b45bc5fdc28',
'end_date' => time(),
'last_updated' => time(),
],
];
$item = new CulturefeedMyProgramItem($values[0]);
$item->save();
$item = new CulturefeedMyProgramItem($values[1]);
$item->save();
$my_program = new CulturefeedMyProgram($user);
$items = $my_program->getItems();
$this->assertTrue(isset($items[$values[0]['item_id']]));
$this->assertTrue(isset($items[$values[1]['item_id']]));
}
/**
* Test delete all items.
*/
public function testDeleteAllItems() {
$user = new stdClass();
$user->uid = 666;
$values = [
[
'uid' => $user->uid,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
],
[
'uid' => $user->uid,
'item_id' => '54ce03c7-4c89-4867-bf66-1b45bc5fdc28',
'end_date' => time(),
'last_updated' => time(),
],
];
$item = new CulturefeedMyProgramItem($values[0]);
$item->save();
$item = new CulturefeedMyProgramItem($values[1]);
$item->save();
$my_program = new CulturefeedMyProgram($user);
$my_program->deleteAllItems();
$this->assertTrue(empty($my_program->getItems()), 'Items are cleared after delete.');
$this->assertNull(CulturefeedMyProgramItem::load(666, $values[0]['item_id']), 'Item one deleted from DB.');
$this->assertNull(CulturefeedMyProgramItem::load(666, $values[1]['item_id']), 'Item two deleted from DB.');
$db_my_program = new CulturefeedMyProgram($user);
$this->assertTrue(empty($db_my_program->getItems()), 'No items left in DB.');
}
}
/**
* Test my program page.
*/
class CulturefeedMyProgramPageTestCase extends CulturefeedMyProgramWebTestCase {
/**
* Info.
*/
public static function getInfo() {
return [
'name' => 'Test my program page',
'description' => 'Test items overview on the my program page',
'group' => 'CulturefeedMyProgram',
];
}
/**
* Test culturefeed_my_program_page() output.
*/
public function testCulturefeedMyProgramPageEmptyTest() {
global $user;
$user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($user);
$this->drupalGet('my-program');
$this->assertResponse(200, 'User is allowed to view my program.');
$this->assertText('No items have been added to your program yet.', 'User gets text to indicate my program is empty.');
$string = self::randomName();
variable_set('culturefeed_my_program_empty_text', $string);
$this->drupalGet('my-program');
$this->assertText($string, 'User gets empty text from variable.');
}
/**
* Test culturefeed_my_program_page() output.
*/
public function testCulturefeedMyProgramPageItems() {
global $user;
$user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($user);
$values = [
[
'uid' => $user->uid,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time(),
'last_updated' => time(),
],
[
'uid' => $user->uid,
'item_id' => '54ce03c7-4c89-4867-bf66-1b45bc5fdc28',
'end_date' => time(),
'last_updated' => time(),
],
];
$item = new CulturefeedMyProgramItem($values[0]);
$item->save();
$item = new CulturefeedMyProgramItem($values[1]);
$item->save();
// Mock culturefeed_search_item_load to avoid callbacks to the API.
$items = &drupal_static('culturefeed_search_item_load', []);
$items[$values[0]['item_id']] = NULL;
$items[$values[1]['item_id']] = NULL;
module_load_include('inc', 'culturefeed_my_program', 'culturefeed_my_program.page');
$output = culturefeed_my_program_page();
$this->assertTrue(isset($output['#items']['item-64ce03c7-4c89-4867-bf66-1b45bc5fdc27']), 'Item one is found');
$this->assertTrue(isset($output['#items']['item-54ce03c7-4c89-4867-bf66-1b45bc5fdc28']), 'Item two is found');
}
}
/**
* Test my program page.
*/
class CulturefeedMyProgramCronTestCase extends CulturefeedMyProgramWebTestCase {
/**
* Info.
*/
public static function getInfo() {
return [
'name' => 'Test cron hook',
'description' => 'Test cron functions (e.g. DB maintenance)',
'group' => 'CulturefeedMyProgram',
];
}
/**
* Test _culturefeed_my_program_cron_delete_passed_events().
*/
public function testCulturefeedMyProgramCronDeletePassedEvents() {
$values = [
[
'uid' => 666,
'item_id' => '64ce03c7-4c89-4867-bf66-1b45bc5fdc27',
'end_date' => time() - 600,
'last_updated' => time(),
],
[
'uid' => 666,
'item_id' => '54ce03c7-4c89-4867-bf66-1b45bc5fdc28',
'end_date' => time() - 600,
'last_updated' => time() - 1200,
],
[
'uid' => 666,
'item_id' => '44ce03c7-4c89-4867-bf66-1b45bc5fdc29',
'end_date' => time() + 600,
'last_updated' => time(),
],
];
$item = new CulturefeedMyProgramItem($values[0]);
$item->save();
$item = new CulturefeedMyProgramItem($values[1]);
$item->save();
$item = new CulturefeedMyProgramItem($values[2]);
$item->save();
_culturefeed_my_program_cron_delete_passed_events(600);
$this->assertNull(CulturefeedMyProgramItem::load(666, $values[0]['item_id']), 'Item one deleted from DB.');
// Item two should still be in the DB because last_updated time is to old.
$item2 = CulturefeedMyProgramItem::load(666, $values[1]['item_id']);
$this->assertEqual(CulturefeedMyProgramItem::class, get_class($item2), 'Item two still in DB.');
// Item three should still be in the DB because the end_date is still in the
// future.
$item3 = CulturefeedMyProgramItem::load(666, $values[2]['item_id']);
$this->assertEqual(CulturefeedMyProgramItem::class, get_class($item3), 'Item three still in DB.');
}
}