aboutsummaryrefslogtreecommitdiffstats
path: root/tests/notification/notification.php
blob: 0bb69dc2c5aa188ba81f50568bef6e76d8425bf2 (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

class phpbb_notification_test extends phpbb_database_test_case
{
	protected $notifications;
	static private $copied_files = array();
	static private $helper;

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/notification.xml');
	}

	/**
	* This should only be called once before the tests are run.
	* This is used to copy the extensions to the phpBB install
	*/
	static public function setUpBeforeClass()
	{
		global $phpbb_root_path;

		parent::setUpBeforeClass();

		self::$helper = new phpbb_test_case_helpers(self);

		// First, move any extensions setup on the board to a temp directory
		self::$copied_files = self::$helper->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/');

		// Then empty the ext/ directory on the board (for accurate test cases)
		self::$helper->empty_dir($phpbb_root_path . 'ext/');

		// Copy our ext/ files from the test case to the board
		self::$copied_files = array_merge(self::$copied_files, self::$helper->copy_dir(dirname(__FILE__) . '/ext/', $phpbb_root_path . 'ext/'));
	}

	/**
	* This should only be called once after the tests are run.
	* This is used to remove the files copied to the phpBB install
	*/
	static public function tearDownAfterClass()
	{
		global $phpbb_root_path;

		// Copy back the board installed extensions from the temp directory
		self::$helper->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/');

		self::$copied_files[] = $phpbb_root_path . 'store/temp_ext/';

		// Remove all of the files we copied around (from board ext -> temp_ext, from test ext -> board ext)
		self::$helper->remove_files(self::$copied_files);
	}

	protected function setUp()
	{
		parent::setUp();

		global $phpbb_root_path, $db, $phpEx;

		if (!function_exists('set_var'))
		{
			include($phpbb_root_path . 'includes/functions.' . $phpEx);
		}

		$db = $this->new_dbal();
		$config = new phpbb_config(array());
		$user = new phpbb_mock_user();

		$this->notifications = new phpbb_notification_manager(
			$db,
			new phpbb_mock_cache(),
			new phpbb_template($phpbb_root_path, $phpEx, $config, $user, new phpbb_style_resource_locator()),
			new phpbb_mock_extension_manager($phpbb_root_path,
				array(
					'test' => array(
						'ext_name' => 'test',
						'ext_active' => '1',
						'ext_path' => 'ext/test/',
					),
				)
			),
			$user,
			new phpbb_mock_notifications_auth(),
			$config,
			$phpbb_root_path,
			$phpEx
		);
	}

	public function test_get_subscription_types()
	{
		$subscription_types = $this->notifications->get_subscription_types();

		$this->assertArrayHasKey('NOTIFICATION_GROUP_MISCELLANEOUS', $subscription_types);
		$this->assertArrayHasKey('NOTIFICATION_GROUP_POSTING', $subscription_types);

		$this->assertArrayHasKey('phpbb_notification_type_bookmark', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('phpbb_notification_type_post', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('phpbb_notification_type_quote', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('phpbb_notification_type_topic', $subscription_types['NOTIFICATION_GROUP_POSTING']);

		$this->assertArrayHasKey('moderation_queue', $subscription_types['NOTIFICATION_GROUP_POSTING']);

		$this->assertArrayHasKey('phpbb_notification_type_pm', $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);
		$this->assertArrayHasKey('phpbb_ext_test_notification_type_test', $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);

		//get_subscription_types
		//get_subscription_methods
	}

	public function test_subscriptions()
	{
		$this->notifications->add_subscription('phpbb_notification_type_post', 0, '');
		$this->notifications->add_subscription('phpbb_notification_type_post', 0, '', 1);
		$this->notifications->add_subscription('phpbb_notification_type_quote', 0, '', 1);

		$this->notifications->add_subscription('phpbb_notification_type_post', 0, '', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 0, 'email', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 0, 'jabber', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 1, '', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 1, 'email', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 1, 'jabber', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 2, '', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 2, 'email', 2);
		$this->notifications->add_subscription('phpbb_notification_type_post', 2, 'jabber', 2);

		$this->assertEquals(array(
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 0,
				'method'		=> '',
			),
		), $this->notifications->get_subscriptions());

		$this->assertEquals(array(
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 1,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_quote',
				'item_id'		=> 0,
				'user_id'		=> 1,
				'method'		=> '',
			),
		), $this->notifications->get_subscriptions(1));

		$this->assertEquals(array(
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 2,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 2,
				'method'		=> 'email',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 2,
				'method'		=> 'jabber',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 1,
				'user_id'		=> 2,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 1,
				'user_id'		=> 2,
				'method'		=> 'email',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 1,
				'user_id'		=> 2,
				'method'		=> 'jabber',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 2,
				'user_id'		=> 2,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 2,
				'user_id'		=> 2,
				'method'		=> 'email',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 2,
				'user_id'		=> 2,
				'method'		=> 'jabber',
			),
		), $this->notifications->get_subscriptions(2));

		$this->assertEquals(array(
			'phpbb_notification_type_post' => array(
				'',
				'email',
				'jabber',
			),
		), $this->notifications->get_subscriptions(2, true));

		$this->notifications->delete_subscription('phpbb_notification_type_post', 0, '', 2);
		$this->notifications->delete_subscription('phpbb_notification_type_post', 1, 'email', 2);
		$this->notifications->delete_subscription('phpbb_notification_type_post', 2, 'jabber', 2);

		$this->assertEquals(array(
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 2,
				'method'		=> 'email',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 0,
				'user_id'		=> 2,
				'method'		=> 'jabber',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 1,
				'user_id'		=> 2,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 1,
				'user_id'		=> 2,
				'method'		=> 'jabber',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 2,
				'user_id'		=> 2,
				'method'		=> '',
			),
			array(
				'item_type'		=> 'phpbb_notification_type_post',
				'item_id'		=> 2,
				'user_id'		=> 2,
				'method'		=> 'email',
			),
		), $this->notifications->get_subscriptions(2));
	}

	public function test_notifications()
	{
		global $db;

		$this->notifications->add_subscription('phpbb_ext_test_notification_type_test');

		// Used to test post notifications later
		$db->sql_query('INSERT INTO ' . TOPICS_WATCH_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'topic_id'			=> 2,
			'notify_status'		=> NOTIFY_YES,
			'user_id'			=> 0,
		)));

		$this->assertEquals(array(
				'notifications'		=> array(),
				'unread_count'		=> 0,
				'total_count'		=> 0,
		), $this->notifications->load_notifications(array(
			'count_unread'	=> true,
		)));

		$this->notifications->add_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '1',
			'topic_id'		=> '1',
			'post_time'		=> 1349413321,
		));

		$this->notifications->add_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '2',
			'topic_id'		=> '2',
			'post_time'		=> 1349413322,
		));

		$this->notifications->add_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '3',
			'topic_id'		=> '2',
			'post_time'		=> 1349413323,
		));

		$this->notifications->add_notifications(array('phpbb_notification_type_quote', 'phpbb_notification_type_bookmark', 'phpbb_notification_type_post', 'phpbb_ext_test_notification_type_test'), array(
			'post_id'		=> '4',
			'topic_id'		=> '2',
			'post_time'		=> 1349413324,
			'poster_id'		=> 2,
			'topic_title'	=> 'test-title',
			'post_subject'	=> 'Re: test-title',
			'forum_id'		=> 2,
			'forum_name'	=> 'Your first forum',
		));

		$db->sql_query('INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'topic_id'			=> 2,
			'user_id'			=> 0,
		)));
		$this->notifications->add_subscription('phpbb_notification_type_bookmark');

		$this->notifications->add_notifications(array('phpbb_notification_type_quote', 'phpbb_notification_type_bookmark', 'phpbb_notification_type_post', 'phpbb_ext_test_notification_type_test'), array(
			'post_id'		=> '5',
			'topic_id'		=> '2',
			'post_time'		=> 1349413325,
			'poster_id'		=> 2,
			'topic_title'	=> 'test-title',
			'post_subject'	=> 'Re: test-title',
			'forum_id'		=> 2,
			'forum_name'	=> 'Your first forum',
		));

		$this->notifications->delete_subscription('phpbb_ext_test_notification_type_test');

		$this->notifications->add_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '6',
			'topic_id'		=> '2',
			'post_time'		=> 1349413326,
		));

		$notifications = $this->notifications->load_notifications(array(
			'count_unread'	=> true,
		));

		$expected = array(
			1 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 1,
				'item_parent_id'	=> 1,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413321,
				'data'				=> array(),
			),
			2 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 2,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413322,
				'data'				=> array(),
			),
			3 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 3,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413323,
				'data'				=> array(),
			),
			4 => array(
				'item_type'			=> 'phpbb_notification_type_post',
				'item_id'			=> 4,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413324,
				'data'				=> array(
					'poster_id'		=> 2,
					'topic_title'	=> 'test-title',
					'post_subject'	=> 'Re: test-title',
					'post_username'	=> '',
					'forum_id'		=> 2,
					'forum_name'	=> 'Your first forum',
				),
			),
			5 => array(
				'item_type'			=> 'phpbb_notification_type_bookmark',
				'item_id'			=> 5,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413325,
				'data'				=> array(
					'poster_id'		=> 2,
					'topic_title'	=> 'test-title',
					'post_subject'	=> 'Re: test-title',
					'post_username'	=> '',
					'forum_id'		=> 2,
					'forum_name'	=> 'Your first forum',
				),
			),
		);

		$this->assertEquals(sizeof($expected), $notifications['unread_count']);

		$notifications = $notifications['notifications'];

		foreach ($expected as $notification_id => $notification_data)
		{
			//echo $notifications[$notification_id];

			$this->assertEquals($notification_id, $notifications[$notification_id]->notification_id, 'notification_id');

			foreach ($notification_data as $key => $value)
			{
				$this->assertEquals($value, $notifications[$notification_id]->$key, $key . ' ' . $notification_id);
			}
		}

		// Now test updating -------------------------------

		$this->notifications->update_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '1',
			'topic_id'		=> '2', // change parent_id
			'post_time'		=> 1349413321,
		));

		$this->notifications->update_notifications('phpbb_ext_test_notification_type_test', array(
			'post_id'		=> '3',
			'topic_id'		=> '2',
			'post_time'		=> 1234, // change time
		));

		$this->notifications->update_notifications(array('phpbb_notification_type_quote', 'phpbb_notification_type_bookmark', 'phpbb_notification_type_post', 'phpbb_ext_test_notification_type_test'), array(
			'post_id'		=> '5',
			'topic_id'		=> '2',
			'poster_id'		=> 2,
			'topic_title'	=> 'test-title2', // change topic_title
			'post_subject'	=> 'Re: test-title2', // change post_subject
			'forum_id'		=> 3, // change forum_id
			'forum_name'	=> 'Your second forum', // change forum_name
		));

		$notifications = $this->notifications->load_notifications(array(
			'count_unread'	=> true,
		));

		$expected = array(
			1 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 1,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413321,
				'data'				=> array(),
			),
			2 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 2,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413322,
				'data'				=> array(),
			),
			3 => array(
				'item_type'			=> 'phpbb_ext_test_notification_type_test',
				'item_id'			=> 3,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1234,
				'data'				=> array(),
			),
			4 => array(
				'item_type'			=> 'phpbb_notification_type_post',
				'item_id'			=> 4,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413324,
				'data'				=> array(
					'poster_id'		=> 2,
					'topic_title'	=> 'test-title',
					'post_subject'	=> 'Re: test-title',
					'post_username'	=> '',
					'forum_id'		=> 2,
					'forum_name'	=> 'Your first forum',
				),
			),
			5 => array(
				'item_type'			=> 'phpbb_notification_type_bookmark',
				'item_id'			=> 5,
				'item_parent_id'	=> 2,
				'user_id'	   		=> 0,
				'unread'	   		=> 1,
				'time'	   			=> 1349413325,
				'data'				=> array(
					'poster_id'		=> 2,
					'topic_title'	=> 'test-title2',
					'post_subject'	=> 'Re: test-title2',
					'post_username'	=> '',
					'forum_id'		=> 3,
					'forum_name'	=> 'Your second forum',
				),
			),
		);

		$this->assertEquals(sizeof($expected), $notifications['unread_count']);

		$notifications = $notifications['notifications'];

		foreach ($expected as $notification_id => $notification_data)
		{
			//echo $notifications[$notification_id];

			$this->assertEquals($notification_id, $notifications[$notification_id]->notification_id, 'notification_id');

			foreach ($notification_data as $key => $value)
			{
				$this->assertEquals($value, $notifications[$notification_id]->$key, $key . ' ' . $notification_id);
			}
		}
	}
}