queueTest.php

  1. 8.0.x tests/queueTest.php
  2. 6.x tests/queueTest.php
  3. 7.x tests/queueTest.php
  4. 5.x tests/queueTest.php
  5. master tests/queueTest.php

Namespace

Unish

Classes

Namesort descending Description
QueueCase @group commands

File

tests/queueTest.php
View source
  1. <?php
  2. namespace Unish;
  3. /**
  4. * @group commands
  5. */
  6. class QueueCase extends CommandUnishTestCase {
  7. function testQueue() {
  8. if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
  9. $this->markTestSkipped("Queue API not available in Drupal 6.");
  10. }
  11. if (UNISH_DRUPAL_MAJOR_VERSION == 7) {
  12. $expected = 'aggregator_feeds,%items,SystemQueue';
  13. }
  14. else {
  15. $expected = 'aggregator_feeds,%items,Drupal\Core\Queue\DatabaseQueue';
  16. }
  17. $sites = $this->setUpDrupal(1, TRUE);
  18. $options = array(
  19. 'yes' => NULL,
  20. 'root' => $this->webroot(),
  21. 'uri' => key($sites),
  22. );
  23. // Enable aggregator since it declares a queue.
  24. $this->drush('pm-enable', array('aggregator'), $options);
  25. $this->drush('queue-list', array(), $options);
  26. $output = $this->getOutput();
  27. $this->assertContains('aggregator_feeds', $output, 'Queue list shows the declared queue.');
  28. $this->drush('php-script', array('queue_script-D' . UNISH_DRUPAL_MAJOR_VERSION), $options + array('script-path' => dirname(__FILE__) . '/resources'));
  29. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  30. $output = trim($this->getOutput());
  31. $parts = explode(",", $output);
  32. $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
  33. $output = $this->getOutput();
  34. $this->drush('queue-run', array('aggregator_feeds'), $options);
  35. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  36. $output = trim($this->getOutput());
  37. $parts = explode(",", $output);
  38. $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue item processed.');
  39. }
  40. /**
  41. * Tests the queue-delete command.
  42. */
  43. public function testQueueDelete() {
  44. if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
  45. $this->markTestSkipped("Queue API not available in Drupal 6.");
  46. }
  47. if (UNISH_DRUPAL_MAJOR_VERSION == 7) {
  48. $expected = 'aggregator_feeds,%items,SystemQueue';
  49. }
  50. else {
  51. $expected = 'aggregator_feeds,%items,Drupal\Core\Queue\DatabaseQueue';
  52. }
  53. $sites = $this->setUpDrupal(1, TRUE);
  54. $options = array(
  55. 'yes' => NULL,
  56. 'root' => $this->webroot(),
  57. 'uri' => key($sites),
  58. );
  59. // Enable aggregator since it declares a queue.
  60. $this->drush('pm-enable', array('aggregator'), $options);
  61. // Add another item to the queue and make sure it was deleted.
  62. $this->drush('php-script', array('queue_script-D' . UNISH_DRUPAL_MAJOR_VERSION), $options + array('script-path' => dirname(__FILE__) . '/resources'));
  63. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  64. $output = trim($this->getOutput());
  65. $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
  66. $this->drush('queue-delete', array('aggregator_feeds'), $options);
  67. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  68. $output = trim($this->getOutput());
  69. $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue was successfully deleted.');
  70. }
  71. /**
  72. * Tests the RequeueException.
  73. */
  74. public function testRequeueException() {
  75. if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
  76. $this->markTestSkipped("RequeueException only available in Drupal 8.");
  77. }
  78. $sites = $this->setUpDrupal(1, TRUE);
  79. $options = array(
  80. 'yes' => NULL,
  81. 'root' => $this->webroot(),
  82. 'uri' => key($sites),
  83. );
  84. // Copy the 'woot' module over to the Drupal site we just set up.
  85. $this->setupModulesForTests($this->webroot());
  86. // Enable woot module, which contains a queue worker that throws a
  87. // RequeueException.
  88. $this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
  89. // Add an item to the queue.
  90. $this->drush('php-script', array('requeue_script'), $options + array('script-path' => dirname(__FILE__) . '/resources'));
  91. // Check that the queue exists and it has one item in it.
  92. $expected = 'woot_requeue_exception,%items,Drupal\Core\Queue\DatabaseQueue';
  93. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  94. $output = trim($this->getOutput());
  95. $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
  96. // Process the queue.
  97. $this->drush('queue-run', array('woot_requeue_exception'), $options);
  98. // Check that the item was processed after being requeued once.
  99. // Here is the detailed workflow of what the above command did.
  100. // 1. Drush calls drush queue-run woot_requeue_exception.
  101. // 2. Drush claims the item. The worker sets a state variable (see below)
  102. // and throws the RequeueException.
  103. // 3. Drush catches the exception and puts it back in the queue.
  104. // 4. Drush claims the next item, which is the one that we just requeued.
  105. // 5. The worker finds the state variable, so it does not throw the
  106. // RequeueException this time (see below).
  107. // 6. Drush removes the item from the queue.
  108. // 7. Command finishes. The queue is empty.
  109. $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  110. $output = trim($this->getOutput());
  111. $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue item processed after being requeued.');
  112. }
  113. /**
  114. * Copies the woot module into Drupal.
  115. *
  116. * @param string $root
  117. * The path to the root directory of Drupal.
  118. */
  119. public function setupModulesForTests($root) {
  120. $wootModule = __DIR__ . '/resources/modules/d' . UNISH_DRUPAL_MAJOR_VERSION . '/woot';
  121. $modulesDir = "$root/sites/all/modules";
  122. $this->mkdir($modulesDir);
  123. \symlink($wootModule, "$modulesDir/woot");
  124. }
  125. }