privatemsg_limits.test

  1. 7-1 privatemsg_limits/privatemsg_limits.test
  2. 7-2 privatemsg_limits/privatemsg_limits.test
  3. 6-2 privatemsg_limits/privatemsg_limits.test

This file contains tests for the privatemsg limits module

File

privatemsg_limits/privatemsg_limits.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains tests for the privatemsg limits module
  5. */
  6. /**
  7. * Test cases for the privatemsg_limits module.
  8. */
  9. class PrivatemsgLimitsTestCase extends PrivatemsgBaseTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Privatemsg Limits',
  13. 'description' => 'Tests limits on sending and receiving messages.',
  14. 'group' => 'Privatemsg',
  15. );
  16. }
  17. /**
  18. * Install necessary modules.
  19. */
  20. function setUp() {
  21. parent::setUp('privatemsg', 'privatemsg_limits', 'privatemsg_filter');
  22. }
  23. /**
  24. * Test sending limits with threads as limits object.
  25. */
  26. function testSendLimitsMessages() {
  27. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  28. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  29. $this->drupalLogin($admin);
  30. $settings = array(
  31. 'privatemsg_limits_send_enabled' => TRUE,
  32. 'privatemsg_limits_send_amount' => 3,
  33. 'privatemsg_limits_send_amount_role_3' => 5,
  34. );
  35. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  36. $this->drupalLogin($user);
  37. for ($i = 0; $i < 3; $i++) {
  38. // Send three messages.
  39. $edit = array(
  40. 'recipient' => $user->name,
  41. 'subject' => $this->randomName(20),
  42. 'body[value]' => $this->randomName(100),
  43. );
  44. $this->drupalPost('messages/new', $edit, t('Send message'));
  45. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  46. }
  47. // Send a fourth message that fails.
  48. $blocked = array(
  49. 'recipient' => $user->name,
  50. 'subject' => $this->randomName(20),
  51. 'body[value]' => $this->randomName(100),
  52. );
  53. $this->drupalPost('messages/new', $blocked, t('Send message'));
  54. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 3 messages every 1 hour.'));
  55. // Verify that replies can't be sent either.
  56. $this->drupalGet('messages');
  57. $this->clickLink($edit['subject']);
  58. $reply = array(
  59. 'body[value]' => $this->randomName(100),
  60. );
  61. $this->drupalPost(NULL, $reply, t('Send message'));
  62. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 3 messages every 1 hour.'));
  63. // Now the same with admin, he should be able to send 5 messages.
  64. $this->drupalLogin($admin);
  65. for ($i = 0; $i < 5; $i++) {
  66. // Send three messages.
  67. $edit = array(
  68. 'recipient' => $admin->name,
  69. 'subject' => $this->randomName(20),
  70. 'body[value]' => $this->randomName(100),
  71. );
  72. $this->drupalPost('messages/new', $edit, t('Send message'));
  73. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  74. }
  75. // Send a sixth message that fails.
  76. $blocked = array(
  77. 'recipient' => $admin->name,
  78. 'subject' => $this->randomName(20),
  79. 'body[value]' => $this->randomName(100),
  80. );
  81. $this->drupalPost('messages/new', $blocked, t('Send message'));
  82. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 5 messages every 1 hour.'));
  83. // Verify that replies can't be sent either.
  84. $this->drupalGet('messages');
  85. $this->clickLink($edit['subject']);
  86. $reply = array(
  87. 'body[value]' => $this->randomName(100),
  88. );
  89. $this->drupalPost(NULL, $reply, t('Send message'));
  90. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 5 messages every 1 hour.'));
  91. }
  92. /**
  93. * Test sending limits with threads as limits object.
  94. */
  95. function testSendLimitsThreads() {
  96. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  97. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  98. $this->drupalLogin($admin);
  99. $settings = array(
  100. 'privatemsg_limits_send_enabled' => TRUE,
  101. 'privatemsg_limits_send_amount' => 3,
  102. 'privatemsg_limits_send_amount_role_3' => 5,
  103. 'privatemsg_limits_send_object' => 'thread',
  104. );
  105. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  106. $this->drupalLogin($user);
  107. for ($i = 0; $i < 3; $i++) {
  108. // Send three messages.
  109. $edit = array(
  110. 'recipient' => $user->name,
  111. 'subject' => $this->randomName(20),
  112. 'body[value]' => $this->randomName(100),
  113. );
  114. $this->drupalPost('messages/new', $edit, t('Send message'));
  115. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  116. }
  117. // Send a fourth message that fails.
  118. $blocked = array(
  119. 'recipient' => $user->name,
  120. 'subject' => $this->randomName(20),
  121. 'body[value]' => $this->randomName(100),
  122. );
  123. $this->drupalPost('messages/new', $blocked, t('Send message'));
  124. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 3 messages every 1 hour.'));
  125. // Verify that replies can still be sent.
  126. $this->drupalGet('messages');
  127. $this->clickLink($edit['subject']);
  128. $reply = array(
  129. 'body[value]' => $this->randomName(100),
  130. );
  131. $this->drupalPost(NULL, $reply, t('Send message'));
  132. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  133. // Now the same with admin, he should be able to send 5 messages.
  134. $this->drupalLogin($admin);
  135. for ($i = 0; $i < 5; $i++) {
  136. // Send three messages.
  137. $edit = array(
  138. 'recipient' => $admin->name,
  139. 'subject' => $this->randomName(20),
  140. 'body[value]' => $this->randomName(100),
  141. );
  142. $this->drupalPost('messages/new', $edit, t('Send message'));
  143. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  144. }
  145. // Send a sixth message that fails.
  146. $blocked = array(
  147. 'recipient' => $admin->name,
  148. 'subject' => $this->randomName(20),
  149. 'body[value]' => $this->randomName(100),
  150. );
  151. $this->drupalPost('messages/new', $blocked, t('Send message'));
  152. $this->assertText(t('Your message was not sent because you have exceeded your sending limit. You are allowed to send 5 messages every 1 hour.'));
  153. // Verify that replies can't be sent either.
  154. $this->drupalGet('messages');
  155. $this->clickLink($edit['subject']);
  156. $reply = array(
  157. 'body[value]' => $this->randomName(100),
  158. );
  159. $this->drupalPost(NULL, $reply, t('Send message'));
  160. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  161. }
  162. /**
  163. * Test receive limit with messages as limits object.
  164. */
  165. function testReceiveLimitsMessages() {
  166. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  167. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  168. $this->drupalLogin($admin);
  169. $settings = array(
  170. 'privatemsg_limits_receive_enabled' => TRUE,
  171. 'privatemsg_limits_receive_amount' => 3,
  172. 'privatemsg_limits_receive_amount_role_3' => 5,
  173. );
  174. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  175. // Check empty inbox.
  176. $this->drupalGet('messages');
  177. $this->assertText(t('You are currently using @percent% (@used messages) of your @limit messages limit.', array('@percent' => 0, '@used' => 0, '@limit' => 5)));
  178. $this->drupalLogin($user);
  179. $this->drupalGet('messages');
  180. $this->assertText(t('You are currently using @percent% (@used messages) of your @limit messages limit.', array('@percent' => 0, '@used' => 0, '@limit' => 3)));
  181. // Send three messages from user to admin.
  182. for ($i = 0; $i < 3; $i++) {
  183. $edit = array(
  184. 'recipient' => $admin->name,
  185. 'subject' => $this->randomName(20),
  186. 'body[value]' => $this->randomName(100),
  187. );
  188. $this->drupalPost('messages/new', $edit, t('Send message'));
  189. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  190. }
  191. // Try sending an additional message.
  192. $blocked = array(
  193. 'recipient' => $admin->name,
  194. 'subject' => $this->randomName(20),
  195. 'body[value]' => $this->randomName(100),
  196. );
  197. $this->drupalPost('messages/new', $blocked, t('Send message'));
  198. $this->assertText(t("Your message mailbox is currently full. You are allowed a maximum of @limit messages in your mailbox at one time. You won't be able to send or receive new messages until you delete some existing ones.", array('@limit' => 3)), 'Limit exceeded message displayed.');
  199. // Try to reply to a sent message.
  200. $this->drupalGet('messages/sent');
  201. $this->clickLink($edit['subject']);
  202. $reply = array(
  203. 'body[value]' => $this->randomName(100),
  204. );
  205. $this->drupalPost(NULL, $reply, t('Send message'));
  206. $this->assertText(t("Your message mailbox is currently full. You are allowed a maximum of @limit messages in your mailbox at one time. You won't be able to send or receive new messages until you delete some existing ones.", array('@limit' => 3)), 'Limit exceeded message displayed.');
  207. // Check user limits.
  208. $this->drupalGet('messages');
  209. $this->assertText(t('You are currently using @percent% (@used messages) of your @limit messages limit.', array('@percent' => 100, '@used' => 3, '@limit' => 3)));
  210. // Check admin limits.
  211. $this->drupalLogin($admin);
  212. $this->drupalGet('messages');
  213. $this->assertText(t('You are currently using @percent% (@used messages) of your @limit messages limit.', array('@percent' => 60, '@used' => 3, '@limit' => 5)));
  214. // Try to send a message to the user.
  215. $blocked = array(
  216. 'recipient' => $user->name,
  217. 'subject' => $this->randomName(20),
  218. 'body[value]' => $this->randomName(100),
  219. );
  220. $this->drupalPost('messages/new', $blocked, t('Send message'));
  221. $this->assertText(t("This message cannot be sent to @user because @user's mailbox is full.", array('@user' => $user->name)));
  222. // Try to reply to a message.
  223. $this->drupalGet('messages');
  224. $this->clickLink($edit['subject']);
  225. $this->assertNoText(t('Send message'));
  226. }
  227. /**
  228. * Test receive limit with messages as limits object.
  229. */
  230. function testReceiveLimitsThreads() {
  231. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  232. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  233. $this->drupalLogin($admin);
  234. $settings = array(
  235. 'privatemsg_limits_receive_enabled' => TRUE,
  236. 'privatemsg_limits_receive_amount' => 3,
  237. 'privatemsg_limits_receive_amount_role_3' => 5,
  238. 'privatemsg_limits_receive_object' => 'thread',
  239. );
  240. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  241. // Check empty inbox.
  242. $this->drupalGet('messages');
  243. $this->assertText(t('You are currently using @percent% (@used conversations) of your @limit conversations limit.', array('@percent' => 0, '@used' => 0, '@limit' => 5)));
  244. $this->drupalLogin($user);
  245. $this->drupalGet('messages');
  246. $this->assertText(t('You are currently using @percent% (@used conversations) of your @limit conversations limit.', array('@percent' => 0, '@used' => 0, '@limit' => 3)));
  247. // Send three messages from user to admin.
  248. for ($i = 0; $i < 3; $i++) {
  249. $edit = array(
  250. 'recipient' => $admin->name,
  251. 'subject' => $this->randomName(20),
  252. 'body[value]' => $this->randomName(100),
  253. );
  254. $this->drupalPost('messages/new', $edit, t('Send message'));
  255. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  256. }
  257. // Try sending an additional message.
  258. $blocked = array(
  259. 'recipient' => $admin->name,
  260. 'subject' => $this->randomName(20),
  261. 'body[value]' => $this->randomName(100),
  262. );
  263. $this->drupalPost('messages/new', $blocked, t('Send message'));
  264. $this->assertText(t("Your message mailbox is currently full. You are allowed a maximum of @limit conversations in your mailbox at one time. You won't be able to start or receive new conversations until you delete some existing ones.", array('@limit' => 3)), 'Limit exceeded message displayed.');
  265. // Try to reply to a sent message.
  266. $this->drupalGet('messages/sent');
  267. $this->clickLink($edit['subject']);
  268. $reply = array(
  269. 'body[value]' => $this->randomName(100),
  270. );
  271. $this->drupalPost(NULL, $reply, t('Send message'));
  272. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  273. // Check user limits.
  274. $this->drupalGet('messages');
  275. $this->assertText(t('You are currently using @percent% (@used conversations) of your @limit conversations limit.', array('@percent' => 100, '@used' => 3, '@limit' => 3)));
  276. // Check admin limits.
  277. $this->drupalLogin($admin);
  278. $this->drupalGet('messages');
  279. $this->assertText(t('You are currently using @percent% (@used conversations) of your @limit conversations limit.', array('@percent' => 60, '@used' => 3, '@limit' => 5)));
  280. // Try to send a message to the user.
  281. $blocked = array(
  282. 'recipient' => $user->name,
  283. 'subject' => $this->randomName(20),
  284. 'body[value]' => $this->randomName(100),
  285. );
  286. $this->drupalPost('messages/new', $blocked, t('Send message'));
  287. $this->assertText(t("This message cannot be sent to @user because @user's mailbox is full.", array('@user' => $user->name)));
  288. // Try to reply to a message.
  289. $this->drupalGet('messages');
  290. $this->clickLink($edit['subject']);
  291. $reply = array(
  292. 'body[value]' => $this->randomName(100),
  293. );
  294. $this->drupalPost(NULL, $reply, t('Send message'));
  295. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  296. }
  297. /**
  298. * Test limiting the number of recipients.
  299. */
  300. function testRecipientsLimits() {
  301. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  302. $user1 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  303. $user2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  304. $user3 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  305. $this->drupalLogin($admin);
  306. $settings = array(
  307. 'privatemsg_limits_recipients_enabled' => TRUE,
  308. 'privatemsg_limits_recipients_amount' => 1,
  309. 'privatemsg_limits_recipients_amount_role_3' => 2,
  310. );
  311. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  312. // Send a message to a single user.
  313. $edit = array(
  314. 'recipient' => $user1->name,
  315. 'subject' => $this->randomName(20),
  316. 'body[value]' => $this->randomName(100),
  317. );
  318. $this->drupalPost('messages/new', $edit, t('Send message'));
  319. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user1->name)));
  320. // Send a message to two users.
  321. $edit = array(
  322. 'recipient' => $user1->name . ', ' . $user2->name,
  323. 'subject' => $this->randomName(20),
  324. 'body[value]' => $this->randomName(100),
  325. );
  326. $this->drupalPost('messages/new', $edit, t('Send message'));
  327. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user1->name . ', ' . $user2->name)));
  328. // Try sending a message to three users.
  329. $edit = array(
  330. 'recipient' => $user1->name . ', ' . $user2->name . ', ' . $user3->name,
  331. 'subject' => $this->randomName(20),
  332. 'body[value]' => $this->randomName(100),
  333. );
  334. $this->drupalPost('messages/new', $edit, t('Send message'));
  335. $this->assertText(t('You are not allowed to send a message to more than @number recipients.', array('@number' => 2)), 'Not allowed message displayed.');
  336. // Login in as user.
  337. $this->drupalLogin($user1);
  338. // Send a message to a single users.
  339. $edit = array(
  340. 'recipient' => $user2->name,
  341. 'subject' => $this->randomName(20),
  342. 'body[value]' => $this->randomName(100),
  343. );
  344. $this->drupalPost('messages/new', $edit, t('Send message'));
  345. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user2->name)));
  346. // Try sending a message to two users.
  347. $edit = array(
  348. 'recipient' => $user2->name . ', ' . $user3->name,
  349. 'subject' => $this->randomName(20),
  350. 'body[value]' => $this->randomName(100),
  351. );
  352. $this->drupalPost('messages/new', $edit, t('Send message'));
  353. $this->assertText(t('You are not allowed to send a message to more than @number recipients.', array('@number' => 1)), 'Not allowed message displayed.');
  354. }
  355. /**
  356. * Tests for limiting the number of messages per thread.
  357. */
  358. function testNumberOfMessagesBlock() {
  359. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  360. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  361. $this->drupalLogin($admin);
  362. $settings = array(
  363. 'privatemsg_limits_messages_per_thread' => 2,
  364. 'privatemsg_limits_messages_per_thread_action' => 'block-message',
  365. );
  366. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  367. // Send a message to a user.
  368. $edit = array(
  369. 'recipient' => $user->name,
  370. 'subject' => $this->randomName(20),
  371. 'body[value]' => $this->randomName(100),
  372. );
  373. $this->drupalPost('messages/new', $edit, t('Send message'));
  374. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  375. // Send a reply.
  376. $this->drupalLogin($user);
  377. $this->drupalGet('messages');
  378. $this->clickLink($edit['subject']);
  379. $edit = array(
  380. 'body[value]' => $this->randomName(100),
  381. );
  382. $this->drupalPost(NULL, $edit, t('Send message'));
  383. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  384. // Try to send another one.
  385. $edit = array(
  386. 'body[value]' => $this->randomName(100),
  387. );
  388. $this->drupalPost(NULL, $edit, t('Send message'));
  389. $this->assertText(t("This message cannot be sent because the thread already contains @limit messages (the maximum number of messages permitted per thread). To send this message, please create a new message thread.", array('@limit' => 2)));
  390. }
  391. /**
  392. * Tests for limiting the number of messages per thread.
  393. */
  394. function testNumberOfMessagesCreate() {
  395. $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
  396. $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
  397. $this->drupalLogin($admin);
  398. $settings = array(
  399. 'privatemsg_limits_messages_per_thread' => 1,
  400. 'privatemsg_limits_messages_per_thread_action' => 'create-new',
  401. );
  402. $this->drupalPost('admin/config/messaging/privatemsg/limits', $settings, t('Save configuration'));
  403. // Send a message to a user.
  404. $edit = array(
  405. 'recipient' => $user->name,
  406. 'subject' => $this->randomName(20),
  407. 'body[value]' => $this->randomName(100),
  408. );
  409. $this->drupalPost('messages/new', $edit, t('Send message'));
  410. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $user->name)));
  411. // Send a reply.
  412. $this->drupalLogin($user);
  413. $this->drupalGet('messages');
  414. $this->clickLink($edit['subject']);
  415. $reply = array(
  416. 'body[value]' => $this->randomName(100),
  417. );
  418. $this->drupalPost(NULL, $reply, t('Send message'));
  419. $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $admin->name)));
  420. // Make sure we are in a new thread and the original thread body isn't
  421. // displayed anymore.
  422. $this->assertNoText($edit['body[value]']);
  423. $this->assertText($reply['body[value]']);
  424. }
  425. }

Classes

Namesort descending Description
PrivatemsgLimitsTestCase Test cases for the privatemsg_limits module.