privatemsg_filter.test

  1. 1 privatemsg_filter/privatemsg_filter.test
  2. 7-1 privatemsg_filter/privatemsg_filter.test
  3. 7-2 privatemsg_filter/privatemsg_filter.test
  4. 6-2 privatemsg_filter/privatemsg_filter.test

Contains tests for the privatemsg_filter module.

File

privatemsg_filter/privatemsg_filter.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains tests for the privatemsg_filter module.
  5. */
  6. /**
  7. * Test filters, tags and inbox/sent handling.
  8. */
  9. class PrivatemsgFilterTestCase extends PrivatemsgBaseTestCase {
  10. /**
  11. * Implements getInfo().
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => t('Privatemsg Filter functionality.'),
  16. 'description' => t('Test filters, tags and inbox/sent handling'),
  17. 'group' => t('Privatemsg'),
  18. );
  19. }
  20. /**
  21. * Implements setUp().
  22. */
  23. function setUp() {
  24. parent::setUp('privatemsg', 'privatemsg_filter');
  25. }
  26. /**
  27. * Test correct handling of read all permissions.
  28. */
  29. function testInboxSentHandling() {
  30. $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg'));
  31. $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  32. // Create new message.
  33. $edit = array(
  34. 'recipient' => $recipient->name,
  35. 'subject' => $this->randomName(20),
  36. 'body[value]' => $this->randomName(100),
  37. );
  38. $this->drupalLogin($author);
  39. $this->drupalPost('messages/new', $edit, t('Send message'));
  40. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
  41. // Validate that the message is not displayed in the inbox of the author
  42. // but in the sent list.
  43. $this->drupalGet('messages');
  44. $this->assertNoText($edit['subject'], t('Thread not displayed in inbox for author.'));
  45. $this->drupalGet('messages/sent');
  46. $this->assertText($edit['subject'], t('Thread displayed in "Sent messages" for author.'));
  47. $this->drupalGet('messages/list');
  48. $this->assertText($edit['subject'], t('Thread displayed in "All messages" for author.'));
  49. // Write a reply as recipient.
  50. $this->drupalLogin($recipient);
  51. $this->drupalGet('messages');
  52. $this->assertText($edit['subject'], t('Thread displayed in inbox for recipient.'));
  53. $this->drupalGet('messages/sent');
  54. $this->assertNoText($edit['subject'], t('Thread not displayed in "Sent messages" for recipient.'));
  55. $this->drupalGet('messages/list');
  56. $this->assertText($edit['subject'], t('Thread displayed in "All messages." for recipient.'));
  57. // Navigate to the new message.
  58. $this->clickLink($edit['subject']);
  59. $response = array(
  60. 'body[value]' => $this->randomName(100),
  61. );
  62. $this->drupalPost(NULL, $response, t('Send message'));
  63. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $author->name)), t('Message sent confirmation displayed'));
  64. $this->drupalGet('messages/sent');
  65. $this->assertText($edit['subject'], t('Thread displayed in "Sent messages" for recipient.'));
  66. $this->drupalLogin($author);
  67. $this->drupalGet('messages');
  68. $this->assertText($edit['subject'], t('Thread displayed in inbox for author.'));
  69. // Test for bug http://drupal.org/node/617648
  70. // Delete all messages for author.
  71. $delete = array(
  72. 'list[1]' => 1,
  73. );
  74. $this->drupalPost(NULL, $delete, t('Delete'));
  75. $this->assertNoText($edit['subject'], t('Thread has been deleted for author.'));
  76. // Write a reply as recipient.
  77. $this->drupalLogin($recipient);
  78. $this->drupalGet('messages');
  79. // Navigate to the new message.
  80. $this->clickLink($edit['subject']);
  81. $response = array(
  82. 'body[value]' => $this->randomName(100),
  83. );
  84. $this->drupalPost(NULL, $response, t('Send message'));
  85. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $author->name)), t('Message sent confirmation displayed'));
  86. // Check if thread is visible again for author.
  87. $this->drupalLogin($author);
  88. $this->drupalGet('messages');
  89. $this->assertText($edit['subject'], t('Thread displayed again in inbox for author.'));
  90. // Test archiving of messages.
  91. // Delete all messages for author.
  92. $archive = array(
  93. 'list[1]' => 1,
  94. 'operation' => 'archive',
  95. );
  96. $this->drupalPost(NULL, $archive, t('Execute'));
  97. $this->assertText(t('The messages have been archived.'), t('Confirmation message displayed'));
  98. $this->assertNoText($edit['subject'], t('Thread has been removed from inbox.'));
  99. $this->drupalGet('messages/list');
  100. $this->assertText($edit['subject'], t('Thread still displayed in "All messages" list.'));
  101. }
  102. }
  103. /**
  104. * Test filters, tags and inbox/sent handling.
  105. */
  106. class PrivatemsgTagsTestCase extends PrivatemsgBaseTestCase {
  107. /**
  108. * Implements getInfo().
  109. */
  110. public static function getInfo() {
  111. return array(
  112. 'name' => t('Privatemsg Tags functionality.'),
  113. 'description' => t('Test Privatemsg tags use and administration functionality.'),
  114. 'group' => t('Privatemsg'),
  115. );
  116. }
  117. /**
  118. * Implements setUp().
  119. */
  120. function setUp() {
  121. parent::setUp('privatemsg', 'privatemsg_filter');
  122. }
  123. /**
  124. * Create and update tags on a single thread.
  125. */
  126. function testFilterFormSingleThread() {
  127. $webuser = $this->drupalCreateuser(array('read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags'));
  128. // Create a new thread through the api.
  129. $response = privatemsg_new_thread(array($webuser), $this->randomName(10), $this->randomName(20), array('author' => $webuser));
  130. $thread_id = $response['message']->thread_id;
  131. $tags = array($this->randomName(), $this->randomName(), $this->randomName(), $this->randomName());
  132. $edit = array(
  133. 'tags' => $tags[0] . ', ' . $tags[1],
  134. );
  135. $this->drupalLogin($webuser);
  136. $this->drupalGet('messages/view/' . $thread_id);
  137. $this->clickLink(t('Tag this conversation'));
  138. $this->drupalPost(NULL, $edit, t('Tag this conversation'));
  139. $this->assertText($tags[0], t('Found message tag'));
  140. $this->assertText($tags[1], t('Found message tag'));
  141. // Create a another thread through the api.
  142. $response = privatemsg_new_thread(array($webuser), $this->randomName(10), $this->randomName(20), array('author' => $webuser));
  143. $thread_id = $response['message']->thread_id;
  144. $edit = array(
  145. 'tags' => $tags[1] . ', ' . $tags[2],
  146. );
  147. $this->drupalGet('messages/view/' . $thread_id);
  148. $this->clickLink(t('Tag this conversation'));
  149. $this->drupalPost(NULL, $edit, t('Tag this conversation'));
  150. $this->assertText($tags[1], t('Found message tag'));
  151. $this->assertText($tags[2], t('Found message tag'));
  152. // Change tags.
  153. $edit = array(
  154. 'tags' => $tags[0],
  155. );
  156. $this->drupalGet('messages/view/' . $thread_id);
  157. $this->clickLink(t('(modify tags)'));
  158. $this->drupalPost(NULL, $edit, t('Tag this conversation'));
  159. $this->assertText($tags[0], t('Found message tag'));
  160. $this->assertNoText($tags[1], t('Tag has been removed.'));
  161. $this->assertNoText($tags[2], t('Tag has been removed.'));
  162. }
  163. function testTagsAdministration() {
  164. // Create users.
  165. $admin = $this->drupalCreateuser(array('administer privatemsg settings', 'read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags'));
  166. // Prepare data.
  167. $private = array(
  168. 'tag' => $this->randomName(10),
  169. 'public' => FALSE,
  170. );
  171. $public = array(
  172. 'tag' => $this->randomName(10),
  173. 'public' => 1,
  174. );
  175. $to_edit = array(
  176. 'tag' => $this->randomName(10),
  177. 'public' => 1,
  178. );
  179. $edited_tag = array(
  180. 'tag' => $this->randomName(10),
  181. 'public' => FALSE,
  182. );
  183. $duplicate = $private;
  184. $this->drupalLogin($admin);
  185. // Check that the empty message is displayed.
  186. $this->drupalGet('admin/config/messaging/privatemsg/tags/list');
  187. $this->assertText(t('No tags available.'), t('No tags exist yet.'));
  188. // Create tags.
  189. $this->drupalPost('admin/config/messaging/privatemsg/tags/add', $private, t('Create tag'));
  190. $this->assertText(t('Tag created.'));
  191. $this->drupalPost('admin/config/messaging/privatemsg/tags/add', $public, t('Create tag'));
  192. $this->assertText(t('Tag created.'));
  193. $this->drupalPost('admin/config/messaging/privatemsg/tags/add', $to_edit, t('Create tag'));
  194. $this->assertText(t('Tag created.'));
  195. $this->drupalPost('admin/config/messaging/privatemsg/tags/add', $duplicate, t('Create tag'));
  196. $this->assertText(t('Tag already exists, choose a different name.'));
  197. // Verify that all tags are displayed.
  198. $this->drupalGet('admin/config/messaging/privatemsg/tags/list');
  199. foreach (array($private, $public, $to_edit) as $tag) {
  200. $this->assertText($tag['tag'], t('Tag %tag displayed', array('%tag' => $tag['tag'])));
  201. }
  202. // Verify private/public flag.
  203. $rows = $this->xpath('//table/tbody/tr');
  204. foreach ($rows as $row) {
  205. // Index 0 is tag name.
  206. if ((string)$row->td[0] == $private['tag']) {
  207. // Index 2 is Yes/- flag indicator.
  208. $this->assertEqual((string)$row->td[2], '-', t('Private tag does not have public flag.'));
  209. }
  210. else {
  211. $this->assertEqual((string)$row->td[2], t('Yes'), t('Public tag does have public flag.'));
  212. }
  213. // Extract edit/delete url. Only the part starting with admin/ is needed.
  214. if ((string)$row->td[0] == $to_edit['tag']) {
  215. $edit_url = substr($row->td[3]->a[0]['href'], strpos($row->td[3]->a[0]['href'], 'admin/'));
  216. }
  217. if ((string)$row->td[0] == $public['tag']) {
  218. $delete_url = drupal_substr($row->td[3]->a[1]['href'], strpos($row->td[3]->a[1]['href'], 'admin/'));
  219. }
  220. }
  221. // Edit Tag.
  222. $this->drupalGet($edit_url);
  223. $this->assertTitle(t('Edit @tag | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'), '@tag' => $to_edit['tag'])), t('Correct title for @tag is set.', array('@tag' => $to_edit['tag'])));
  224. // With duplicate data.
  225. $this->drupalPost(NULL, $duplicate, t('Save tag'));
  226. $this->assertText(t('Tag already exists, choose a different name.'));
  227. // With valid data.
  228. $this->drupalPost(NULL, $edited_tag, t('Save tag'));
  229. $this->assertText(t('Tag updated.'), t('Tag has been updated'));
  230. // Verify edited tag.
  231. $this->assertNoText($to_edit['tag'], t('Old tag name not found anymore.'));
  232. $this->assertText($edited_tag['tag'], t('Tag has been renamed.'));
  233. $rows = $this->xpath('//table/tbody/tr');
  234. foreach ($rows as $row) {
  235. // The new tag name should exist and the public flag should be set to false.
  236. if ((string)$row->td[0] == $edited_tag['tag']) {
  237. $this->assertEqual((string)$row->td[2], '-', t('Edited tag does not have public flag.'));
  238. }
  239. }
  240. // Delete tag.
  241. $this->drupalPost($delete_url, array(), t('Delete'));
  242. $this->assertText(t('Tag has been deleted'), t('Tag has been deleted'));
  243. $this->assertNoText($public['tag'], t('Deleted tag is not displayed anymore.'));
  244. }
  245. /**
  246. * Tests if the tagging feature works when a user doesn't have the filter
  247. * permission.
  248. */
  249. function testOnlyTaggingPermission() {
  250. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  251. $webuser = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'tag private messages', 'create private message tags'));
  252. // Display tag column in thread list.
  253. $this->drupalLogin($admin);
  254. $this->drupalPost('admin/config/messaging/privatemsg', array('privatemsg_display_fields[tags]' => 'tags'), t('Save configuration'));
  255. // Create two threads through the API.
  256. $response = privatemsg_new_thread(array($webuser), $subject1 = $this->randomName(10), $this->randomName(20), array('author' => $admin));
  257. $thread_id1 = $response['message']->thread_id;
  258. $response = privatemsg_new_thread(array($webuser), $subject2 = $this->randomName(10), $this->randomName(20), array('author' => $admin));
  259. $response['message']->thread_id;
  260. // Log in and check that both messages are visible.
  261. $this->drupalLogin($webuser);
  262. $this->drupalGet('messages');
  263. $this->assertText($subject1, t('Message is displayed.'));
  264. $this->assertText($subject2, t('Message is displayed.'));
  265. // Tag first thread.
  266. $tag = array(
  267. 'tag-add' => $this->randomName(5),
  268. 'list[' . $thread_id1 . ']' => $thread_id1,
  269. );
  270. $this->drupalPost(NULL, $tag, t('Apply Tag'));
  271. // Filter by tag, verify that only the first thread is displayed, an
  272. // informal message and no filter form.
  273. $this->clickLink($tag['tag-add']);
  274. $this->assertText(t('Messages tagged with @tags are currently displayed. Click here to remove this filter.', array('@tags' => $tag['tag-add'])), t('Tag filter message displayed.'));
  275. $this->assertNoText(t('Filter messages'));
  276. $this->assertText($subject1, t('First thread displayed.'));
  277. $this->assertNoText($subject2, t('Second thread not displayed.'));
  278. // Check paging, set threads per page to 1.
  279. variable_set('privatemsg_per_page', 1);
  280. // Go the second page, only the second thread should be visible there.
  281. $this->drupalGet('messages');
  282. $this->clickLink('2');
  283. $this->assertNoText($subject1, t('First thread not displayed.'));
  284. $this->assertText($subject2, t('Second thread displayed.'));
  285. // Only the first thread should be visible on the
  286. // first page.
  287. $this->clickLink('1');
  288. $this->assertText($subject1, t('First thread displayed.'));
  289. $this->assertNoText($subject2, t('Second thread not displayed.'));
  290. // Now, filter by tag (which should be visible on this page) and verify
  291. // that there is no pager shown.
  292. $this->clickLink($tag['tag-add']);
  293. $this->assertText(t('Messages tagged with @tags are currently displayed. Click here to remove this filter.', array('@tags' => $tag['tag-add'])), t('Tag filter message displayed.'));
  294. $this->assertNoText(t('Filter messages'));
  295. $this->assertText($subject1, t('First thread displayed.'));
  296. $this->assertNoText($subject2, t('Second thread not displayed.'));
  297. $this->assertNoLink('2');
  298. }
  299. function testInboxTagging() {
  300. $webuser = $this->drupalCreateuser(array('read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags'));
  301. $admin = $this->drupalCreateUser(array('administer privatemsg settings'));
  302. // Display tag column in thread list.
  303. $this->drupalLogin($admin);
  304. $this->drupalPost('admin/config/messaging/privatemsg', array('privatemsg_display_fields[tags]' => 'tags'), t('Save configuration'));
  305. // Create a new thread through the api.
  306. $response = privatemsg_new_thread(array($webuser), $subject1 = $this->randomName(10), $this->randomName(20), array('author' => $webuser));
  307. $thread_id = $response['message']->thread_id;
  308. $tag1 = $this->randomName();
  309. $tag2 = $this->randomName();
  310. $edit = array(
  311. 'tags' => $tag1 . ', ' . $tag2,
  312. );
  313. $this->drupalLogin($webuser);
  314. $this->drupalGet('messages/view/' . $thread_id);
  315. $this->clickLink(t('Tag this conversation'));
  316. $this->drupalPost(NULL, $edit, t('Tag this conversation'));
  317. $this->assertText($tag1, t('Found message tag'));
  318. $this->assertText($tag2, t('Found message tag'));
  319. // Create another thread.
  320. $response = privatemsg_new_thread(array($webuser), $subject2 = $this->randomName(10), $this->randomName(20), array('author' => $webuser));
  321. $thread_id2 = $response['message']->thread_id;
  322. $this->drupalGet('messages');
  323. $rows = $this->xpath('//tbody/tr');
  324. foreach ($rows as $row) {
  325. if ($row->td[2]->a == $subject1) {
  326. // The first thread should have both tags. Try both ways as the order
  327. // might change.
  328. $verify = (($tag1 == $row->td[1]->a[0] && $tag2 == $row->td[1]->a[1]) || ($tag1 == $row->td[1]->a[1] && $tag2 == $row->td[1]->a[0]));
  329. $this->assertTrue($verify, t('First thread is correctly tagged.'));
  330. }
  331. if ($row->td[2]->a == $subject2) {
  332. // The second thread should have no tags.
  333. $this->assertEqual('', $row->td[1], t('Second thread is not tagged.'));
  334. }
  335. }
  336. $add_tag = array(
  337. 'list[' . $thread_id2 . ']' => 1,
  338. 'tag-add' => $tag2,
  339. );
  340. $this->drupalPost(NULL, $add_tag, t('Apply Tag'));
  341. $rows = $this->xpath('//tbody/tr');
  342. foreach ($rows as $row) {
  343. if ($row->td[2]->a == $subject1) {
  344. // The first thread should have both tags. Try both ways as the order
  345. // might change.
  346. $verify = (($tag1 == $row->td[1]->a[0] && $tag2 == $row->td[1]->a[1]) || ($tag1 == $row->td[1]->a[1] && $tag2 == $row->td[1]->a[0]));
  347. $this->assertTrue($verify, t('First thread is correctly tagged.'));
  348. }
  349. if ($row->td[2]->a == $subject2) {
  350. // The second thread should have one tag.
  351. $this->assertEqual($tag2, $row->td[1]->a, t('Second thread is correctly tagged.'));
  352. }
  353. }
  354. $remove_tag = array(
  355. 'list[' . $thread_id . ']' => 1,
  356. 'list[' . $thread_id2 . ']' => 1,
  357. 'tag-remove' => 3,
  358. );
  359. $this->drupalPost(NULL, $remove_tag, t('Remove Tag'));
  360. $rows = $this->xpath('//tbody/tr');
  361. foreach ($rows as $row) {
  362. if ($row->td[2]->a == $subject1) {
  363. // The first thread should have only one tag now.
  364. $this->assertEqual($tag1, $row->td[1]->a, t('First thread is correctly tagged.'));
  365. }
  366. if ($row->td[2]->a == $subject2) {
  367. // The second thread should have no tags.
  368. $this->assertEqual('', $row->td[1], t('Second thread is not tagged.'));
  369. }
  370. }
  371. $this->assertNoText($tag2, t('Second tag is not displayed anymore.'));
  372. }
  373. }
  374. /**
  375. * Test filters, tags and inbox/sent handling.
  376. */
  377. class PrivatemsgFilterWidgetTestCase extends PrivatemsgBaseTestCase {
  378. /**
  379. * Implements getInfo().
  380. */
  381. public static function getInfo() {
  382. return array(
  383. 'name' => t('Privatemsg filter widget'),
  384. 'description' => t('Tests the Privatemsg filter widget displayed on message listings'),
  385. 'group' => t('Privatemsg'),
  386. );
  387. }
  388. /**
  389. * Implements setUp().
  390. */
  391. function setUp() {
  392. parent::setUp('privatemsg', 'privatemsg_filter');
  393. }
  394. /**
  395. * Generic filter widget tests.
  396. */
  397. function testAuthorSearch() {
  398. $user1 = $this->drupalCreateuser(array('read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags', 'filter private messages'));
  399. $user2 = $this->drupalCreateuser(array('read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags', 'filter private messages'));
  400. $user3 = $this->drupalCreateuser(array('read privatemsg', 'write privatemsg', 'tag private messages', 'create private message tags', 'filter private messages'));
  401. $this->drupalLogin($user2);
  402. $this->drupalGet('messages');
  403. // Make sure the widget is not displayed when there are no messages.
  404. $this->assertNoFieldById('edit-author');
  405. // Create a new thread from user 1 through the api.
  406. $response = privatemsg_new_thread(array($user2), $subject = $this->randomName(10), $this->randomName(20), array('author' => $user1));
  407. $this->drupalGet('messages');
  408. // Make sure the widget is now displayed and the message is too.
  409. $this->assertText($subject);
  410. $this->assertFieldById('edit-author');
  411. // Search for user 3 which will find no results but the widget should still be displayed.
  412. $this->drupalPost(NULL, array('author' => $user3->name), t('Filter'));
  413. $this->assertNoText($subject);
  414. $this->assertFieldById('edit-author', $user3->name . ', ');
  415. // Reset filter widget.
  416. $this->drupalPost(NULL, array(), t('Reset'));
  417. $this->assertFieldById('edit-author');
  418. $this->assertText($subject);
  419. // Create a new thread through the api.
  420. $response = privatemsg_new_thread(array($user2), $subject2 = $this->randomName(10), $this->randomName(20), array('author' => $user3));
  421. // Make sure that the new message is displayed.
  422. $this->drupalGet('messages');
  423. $this->assertText($subject2);
  424. // Search for user 1 which should only display his message.
  425. $this->drupalPost(NULL, array('author' => $user1->name), t('Filter'));
  426. $this->assertText($subject);
  427. $this->assertNoText($subject2);
  428. // Save the filter and access /messages again - The filter should still be
  429. // active.
  430. $this->drupalPost(NULL, array(), t('Save filter'));
  431. $this->drupalGet('messages');
  432. $this->assertFieldById('edit-author', $user1->name . ', ');
  433. $this->assertText($subject);
  434. $this->assertNoText($subject2);
  435. // Reset filter widget.
  436. $this->drupalPost(NULL, array(), t('Reset'));
  437. $this->assertFieldById('edit-author');
  438. $this->assertText($subject);
  439. $this->assertText($subject2);
  440. }
  441. }

Classes

Namesort descending Description
PrivatemsgFilterTestCase Test filters, tags and inbox/sent handling.
PrivatemsgFilterWidgetTestCase Test filters, tags and inbox/sent handling.
PrivatemsgTagsTestCase Test filters, tags and inbox/sent handling.