/tests/notification/notification_test.php

https://github.com/marc1706/phpbb · PHP · 338 lines · 285 code · 37 blank · 16 comment · 0 complexity · 5c482448fbb29872e0abe9e489a32572 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. require_once __DIR__ . '/base.php';
  14. class phpbb_notification_test extends phpbb_tests_notification_base
  15. {
  16. protected $notifications, $db, $container, $user, $config, $auth, $cache;
  17. public function getDataSet()
  18. {
  19. return $this->createXMLDataSet(__DIR__ . '/fixtures/notification.xml');
  20. }
  21. public function test_get_notification_type_id()
  22. {
  23. // They should be inserted the first time
  24. $post_type_id = $this->notifications->get_notification_type_id('notification.type.post');
  25. $quote_type_id = $this->notifications->get_notification_type_id('notification.type.quote');
  26. $test_type_id = $this->notifications->get_notification_type_id('test');
  27. self::assertEquals(array(
  28. 'test' => $test_type_id,
  29. 'notification.type.quote' => $quote_type_id,
  30. 'notification.type.post' => $post_type_id,
  31. ),
  32. $this->notifications->get_notification_type_ids(array(
  33. 'test',
  34. 'notification.type.quote',
  35. 'notification.type.post',
  36. )
  37. ));
  38. self::assertEquals($quote_type_id, $this->notifications->get_notification_type_id('notification.type.quote'));
  39. try
  40. {
  41. self::assertEquals(false, $this->notifications->get_notification_type_id('fail'));
  42. self::fail('Non-existent type should throw an exception');
  43. }
  44. catch (Exception $e) {}
  45. }
  46. public function test_get_subscription_types()
  47. {
  48. $subscription_types = $this->notifications->get_subscription_types();
  49. self::assertArrayHasKey('NOTIFICATION_GROUP_MISCELLANEOUS', $subscription_types);
  50. self::assertArrayHasKey('NOTIFICATION_GROUP_POSTING', $subscription_types);
  51. self::assertArrayHasKey('notification.type.bookmark', $subscription_types['NOTIFICATION_GROUP_POSTING']);
  52. self::assertArrayHasKey('notification.type.post', $subscription_types['NOTIFICATION_GROUP_POSTING']);
  53. self::assertArrayHasKey('notification.type.quote', $subscription_types['NOTIFICATION_GROUP_POSTING']);
  54. self::assertArrayHasKey('notification.type.topic', $subscription_types['NOTIFICATION_GROUP_POSTING']);
  55. self::assertArrayHasKey('notification.type.pm', $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);
  56. //get_subscription_types
  57. //get_subscription_methods
  58. }
  59. public function test_subscriptions()
  60. {
  61. $expected_subscriptions = array(
  62. 'notification.type.forum' => array('notification.method.board'),
  63. 'notification.type.post' => array('notification.method.board'),
  64. 'notification.type.topic' => array('notification.method.board'),
  65. 'notification.type.quote' => array('notification.method.board'),
  66. 'notification.type.bookmark' => array('notification.method.board'),
  67. 'test' => array('notification.method.board'),
  68. 'notification.type.pm' => array('notification.method.board'),
  69. );
  70. $subscriptions = $this->notifications->get_global_subscriptions(2);
  71. foreach ($expected_subscriptions as $item_type => $methods)
  72. {
  73. self::assertArrayHasKey($item_type, $subscriptions);
  74. $this->assert_array_content_equals($methods, $subscriptions[$item_type]);
  75. }
  76. foreach ($subscriptions as $item_type => $methods)
  77. {
  78. $this->assert_array_content_equals($methods, $expected_subscriptions[$item_type]);
  79. }
  80. $this->notifications->delete_subscription('notification.type.post', 0, 'notification.method.board', 2);
  81. self::assertArrayNotHasKey('notification.type.post', $this->notifications->get_global_subscriptions(2));
  82. $this->notifications->add_subscription('notification.type.post', 0, 'notification.method.board', 2);
  83. self::assertArrayHasKey('notification.type.post', $this->notifications->get_global_subscriptions(2));
  84. }
  85. public function test_notifications()
  86. {
  87. $this->db->sql_query('DELETE FROM phpbb_notification_types');
  88. $types = array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', 'test');
  89. foreach ($types as $id => $type)
  90. {
  91. $this->getConnection()->createQueryTable('insertNotification', 'INSERT INTO phpbb_notification_types ' .
  92. $this->db->sql_build_array('INSERT', array(
  93. 'notification_type_id' => ($id + 1),
  94. 'notification_type_name' => $type,
  95. 'notification_type_enabled' => 1,
  96. ))
  97. );
  98. }
  99. // Used to test post notifications later
  100. $this->db->sql_query('INSERT INTO ' . TOPICS_WATCH_TABLE . ' ' . $this->db->sql_build_array('INSERT', array(
  101. 'topic_id' => 2,
  102. 'notify_status' => NOTIFY_YES,
  103. 'user_id' => 0,
  104. )));
  105. self::assertEquals(array(
  106. 'notifications' => array(),
  107. 'unread_count' => 0,
  108. 'total_count' => 0,
  109. ), $this->notifications->load_notifications('notification.method.board', array(
  110. 'count_unread' => true,
  111. )));
  112. $this->notifications->add_notifications('test', array(
  113. 'post_id' => '1',
  114. 'topic_id' => '1',
  115. 'post_time' => 1349413321,
  116. ));
  117. $this->notifications->add_notifications('test', array(
  118. 'post_id' => '2',
  119. 'topic_id' => '2',
  120. 'post_time' => 1349413322,
  121. ));
  122. $this->notifications->add_notifications('test', array(
  123. 'post_id' => '3',
  124. 'topic_id' => '2',
  125. 'post_time' => 1349413323,
  126. ));
  127. $this->notifications->add_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', 'test'), array(
  128. 'post_id' => '4',
  129. 'topic_id' => '2',
  130. 'post_time' => 1349413324,
  131. 'poster_id' => 2,
  132. 'topic_title' => 'test-title',
  133. 'post_subject' => 'Re: test-title',
  134. 'forum_id' => 2,
  135. 'forum_name' => 'Your first forum',
  136. 'post_username' => '',
  137. 'post_text' => 'test text',
  138. ));
  139. $this->db->sql_query('INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $this->db->sql_build_array('INSERT', array(
  140. 'topic_id' => 2,
  141. 'user_id' => 0,
  142. )));
  143. $this->notifications->add_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', 'test'), array(
  144. 'post_id' => '5',
  145. 'topic_id' => '2',
  146. 'post_time' => 1349413325,
  147. 'poster_id' => 2,
  148. 'topic_title' => 'test-title',
  149. 'post_subject' => 'Re: test-title',
  150. 'forum_id' => 2,
  151. 'forum_name' => 'Your first forum',
  152. 'post_username' => '',
  153. 'post_text' => 'test text',
  154. ));
  155. $this->notifications->delete_subscription('test');
  156. $this->notifications->add_notifications('test', array(
  157. 'post_id' => '6',
  158. 'topic_id' => '2',
  159. 'post_time' => 1349413326,
  160. ));
  161. $this->assert_notifications(
  162. array(
  163. array(
  164. 'item_id' => 1,
  165. 'item_parent_id' => 1,
  166. 'user_id' => 0,
  167. 'notification_read' => 0,
  168. 'notification_time' => 1349413321,
  169. 'notification_data' => array(),
  170. ),
  171. array(
  172. 'item_id' => 2,
  173. 'item_parent_id' => 2,
  174. 'user_id' => 0,
  175. 'notification_read' => 0,
  176. 'notification_time' => 1349413322,
  177. 'notification_data' => array(),
  178. ),
  179. array(
  180. 'item_id' => 3,
  181. 'item_parent_id' => 2,
  182. 'user_id' => 0,
  183. 'notification_read' => 0,
  184. 'notification_time' => 1349413323,
  185. 'notification_data' => array(),
  186. ),
  187. array(
  188. 'item_id' => 4,
  189. 'item_parent_id' => 2,
  190. 'user_id' => 0,
  191. 'notification_read' => 0,
  192. 'notification_time' => 1349413324,
  193. 'notification_data' => array(
  194. 'poster_id' => 2,
  195. 'topic_title' => 'test-title',
  196. 'post_subject' => 'Re: test-title',
  197. 'post_username' => '',
  198. 'forum_id' => 2,
  199. 'forum_name' => 'Your first forum',
  200. ),
  201. ),
  202. array(
  203. 'item_id' => 5,
  204. 'item_parent_id' => 2,
  205. 'user_id' => 0,
  206. 'notification_read' => 0,
  207. 'notification_time' => 1349413325,
  208. 'notification_data' => array(
  209. 'poster_id' => 2,
  210. 'topic_title' => 'test-title',
  211. 'post_subject' => 'Re: test-title',
  212. 'post_username' => '',
  213. 'forum_id' => 2,
  214. 'forum_name' => 'Your first forum',
  215. ),
  216. ),
  217. )
  218. );
  219. // Now test updating -------------------------------
  220. $this->notifications->update_notifications('test', array(
  221. 'post_id' => '1',
  222. 'topic_id' => '2', // change parent_id
  223. 'post_time' => 1349413321,
  224. ));
  225. $this->notifications->update_notifications('test', array(
  226. 'post_id' => '3',
  227. 'topic_id' => '2',
  228. 'post_time' => 1234, // change time
  229. ));
  230. $this->notifications->update_notifications(array('notification.type.quote', 'notification.type.bookmark', 'notification.type.post', 'test'), array(
  231. 'post_id' => '5',
  232. 'topic_id' => '2',
  233. 'poster_id' => 2,
  234. 'topic_title' => 'test-title2', // change topic_title
  235. 'post_subject' => 'Re: test-title2', // change post_subject
  236. 'forum_id' => 3, // change forum_id
  237. 'forum_name' => 'Your second forum', // change forum_name
  238. 'post_username' => '',
  239. 'post_text' => 'test text2',
  240. 'post_time' => 1349413325,
  241. ));
  242. $this->assert_notifications(
  243. array(
  244. array(
  245. 'item_id' => 3,
  246. 'item_parent_id' => 2,
  247. 'user_id' => 0,
  248. 'notification_read' => 0,
  249. 'notification_time' => 1234,
  250. 'notification_data' => array(),
  251. ),
  252. array(
  253. 'item_id' => 1,
  254. 'item_parent_id' => 2,
  255. 'user_id' => 0,
  256. 'notification_read' => 0,
  257. 'notification_time' => 1349413321,
  258. 'notification_data' => array(),
  259. ),
  260. array(
  261. 'item_id' => 2,
  262. 'item_parent_id' => 2,
  263. 'user_id' => 0,
  264. 'notification_read' => 0,
  265. 'notification_time' => 1349413322,
  266. 'notification_data' => array(),
  267. ),
  268. array(
  269. 'item_id' => 4,
  270. 'item_parent_id' => 2,
  271. 'user_id' => 0,
  272. 'notification_read' => 0,
  273. 'notification_time' => 1349413324,
  274. 'notification_data' => array(
  275. 'poster_id' => 2,
  276. 'topic_title' => 'test-title',
  277. 'post_subject' => 'Re: test-title',
  278. 'post_username' => '',
  279. 'forum_id' => 2,
  280. 'forum_name' => 'Your first forum',
  281. ),
  282. ),
  283. array(
  284. 'item_id' => 5,
  285. 'item_parent_id' => 2,
  286. 'user_id' => 0,
  287. 'notification_read' => 0,
  288. 'notification_time' => 1349413325,
  289. 'notification_data' => array(
  290. 'poster_id' => 2,
  291. 'topic_title' => 'test-title2',
  292. 'post_subject' => 'Re: test-title2',
  293. 'post_username' => '',
  294. 'forum_id' => 3,
  295. 'forum_name' => 'Your second forum',
  296. ),
  297. ),
  298. )
  299. );
  300. }
  301. }