aboutsummaryrefslogtreecommitdiffstats
path: root/tests/notification/notification_test.php
blob: 79fa5338c44b3733bb8c2d462505411312f598ef (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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/base.php';

class phpbb_notification_test extends phpbb_tests_notification_base
{
	protected $notifications, $db, $container, $user, $config, $auth, $cache;

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

	public function test_get_notification_type_id()
	{
		// They should be inserted the first time
		$post_type_id = $this->notifications->get_notification_type_id('notification.type.post');
		$quote_type_id = $this->notifications->get_notification_type_id('notification.type.quote');
		$test_type_id = $this->notifications->get_notification_type_id('test');

		$this->assertEquals(array(
				'test'	=> $test_type_id,
				'notification.type.quote'	=> $quote_type_id,
				'notification.type.post'	=> $post_type_id,
			),
			$this->notifications->get_notification_type_ids(array(
				'test',
				'notification.type.quote',
				'notification.type.post',
			)
		));
		$this->assertEquals($quote_type_id, $this->notifications->get_notification_type_id('notification.type.quote'));

		try
		{
			$this->assertEquals(false, $this->notifications->get_notification_type_id('fail'));

			$this->fail('Non-existent type should throw an exception');
		}
		catch (Exception $e) {}
	}

	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('notification.type.bookmark', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('notification.type.post', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('notification.type.quote', $subscription_types['NOTIFICATION_GROUP_POSTING']);
		$this->assertArrayHasKey('notification.type.topic', $subscription_types['NOTIFICATION_GROUP_POSTING']);

		$this->assertArrayHasKey('notification.type.pm', $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);

		//get_subscription_types
		//get_subscription_methods
	}

	public function test_subscriptions()
	{
		$expected_subscriptions = array(
			'notification.type.post'		=> array(''),
			'notification.type.topic'		=> array(''),
			'notification.type.quote'		=> array(''),
			'notification.type.bookmark'	=> array(''),
			'test'		=> array(''),
			'notification.type.pm'		=> array(''),
		);

		$subscriptions = $this->notifications->get_global_subscriptions(2);

		foreach ($expected_subscriptions as $item_type => $methods)
		{
			$this->assert_array_content_equals($methods, $subscriptions[$item_type]);
		}

		foreach ($subscriptions as $item_type => $methods)
		{
			$this->assert_array_content_equals($methods, $expected_subscriptions[$item_type]);	
		}

		$this->notifications->delete_subscription('notification.type.post', 0, '', 2);

		$this->assertArrayNotHasKey('notification.type.post', $this->notifications->get_global_subscriptions(2));

		$this->notifications->add_subscription('notification.type.post', 0, '', 2);

		$this->assertArrayHasKey('notification.type.post', $this->notifications->get_global_subscriptions(2));
	}

	public function test_notifications()
	{
		$this->db->sql_query('DELETE FROM phpbb_notification_types');

		$types = array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', 'test');
		foreach ($types as $id => $type)
		{
			$this->db->sql_query('INSERT INTO phpbb_notification_types ' .
				$this->db->sql_build_array('INSERT', array(
					'notification_type_id'		=> ($id + 1),
					'notification_type_name'	=> $type,
					'notification_type_enabled'	=> 1,
				))
			);
		}

		// Used to test post notifications later
		$this->db->sql_query('INSERT INTO ' . TOPICS_WATCH_TABLE . ' ' . $this->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('test', array(
			'post_id'		=> '1',
			'topic_id'		=> '1',
			'post_time'		=> 1349413321,
		));

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

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

		$this->notifications->add_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', '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',
		));

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

		$this->notifications->add_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', '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('test');

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

		$this->assert_notifications(
			array(
				array(
					'item_id'			=> 1,
					'item_parent_id'	=> 1,
					'user_id'	   		=> 0,
					'notification_read'				=> 0,
					'notification_time'	   			=> 1349413321,
					'notification_data'			   	=> array(),
				),
				array(
					'item_id'			=> 2,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'				=> 0,
					'notification_time'	   			=> 1349413322,
					'notification_data'				=> array(),
				),
				array(
					'item_id'			=> 3,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'				=> 0,
					'notification_time'	   			=> 1349413323,
					'notification_data'			   	=> array(),
				),
				array(
					'item_id'			=> 4,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'				=> 0,
					'notification_time'	   			=> 1349413324,
					'notification_data'			   	=> array(
						'poster_id'		=> 2,
						'topic_title'	=> 'test-title',
						'post_subject'	=> 'Re: test-title',
						'post_username'	=> '',
						'forum_id'		=> 2,
						'forum_name'	=> 'Your first forum',
					),
				),
				array(
					'item_id'			=> 5,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'				=> 0,
					'notification_time'	   			=> 1349413325,
					'notification_data'			   	=> array(
						'poster_id'		=> 2,
						'topic_title'	=> 'test-title',
						'post_subject'	=> 'Re: test-title',
						'post_username'	=> '',
						'forum_id'		=> 2,
						'forum_name'	=> 'Your first forum',
					),
				),
			)
		);

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

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

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

		$this->notifications->update_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', '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
		));

		$this->assert_notifications(
			array(
				array(
					'item_id'			=> 3,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'	=> 0,
					'notification_time'	=> 1234,
					'notification_data'	=> array(),
				),
				array(
					'item_id'			=> 1,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'	=> 0,
					'notification_time'	=> 1349413321,
					'notification_data'	=> array(),
				),
				array(
					'item_id'			=> 2,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'	=> 0,
					'notification_time'	=> 1349413322,
					'notification_data'	=> array(),
				),
				array(
					'item_id'			=> 4,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'	=> 0,
					'notification_time'	=> 1349413324,
					'notification_data'	=> array(
						'poster_id'		=> 2,
						'topic_title'	=> 'test-title',
						'post_subject'	=> 'Re: test-title',
						'post_username'	=> '',
						'forum_id'		=> 2,
						'forum_name'	=> 'Your first forum',
					),
				),
				array(
					'item_id'			=> 5,
					'item_parent_id'	=> 2,
					'user_id'	   		=> 0,
					'notification_read'	=> 0,
					'notification_time'	=> 1349413325,
					'notification_data'	=> array(
						'poster_id'		=> 2,
						'topic_title'	=> 'test-title2',
						'post_subject'	=> 'Re: test-title2',
						'post_username'	=> '',
						'forum_id'		=> 3,
						'forum_name'	=> 'Your second forum',
					),
				),
			)
		);
	}
}