8 class QueueCase extends CommandUnishTestCase {
10 function testQueue() {
11 if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
12 $this->markTestSkipped("Queue API not available in Drupal 6.");
15 if (UNISH_DRUPAL_MAJOR_VERSION == 7) {
16 $expected = 'aggregator_feeds,%items,SystemQueue';
19 $expected = 'aggregator_feeds,%items,Drupal\Core\Queue\DatabaseQueue';
22 $sites = $this->setUpDrupal(1, TRUE);
25 'root' => $this->webroot(),
29 // Enable aggregator since it declares a queue.
30 $this->drush('pm-enable', array('aggregator'), $options);
32 $this->drush('queue-list', array(), $options);
33 $output = $this->getOutput();
34 $this->assertContains('aggregator_feeds', $output, 'Queue list shows the declared queue.');
36 $this->drush('php-script', array('queue_script-D' . UNISH_DRUPAL_MAJOR_VERSION), $options + array('script-path' => dirname(__FILE__) . '/resources'));
37 $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
38 $output = trim($this->getOutput());
39 $parts = explode(",", $output);
40 $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
41 $output = $this->getOutput();
43 $this->drush('queue-run', array('aggregator_feeds'), $options);
44 $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
45 $output = trim($this->getOutput());
46 $parts = explode(",", $output);
47 $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue item processed.');
51 * Tests the RequeueException.
53 public function testRequeueException() {
54 if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
55 $this->markTestSkipped("RequeueException only available in Drupal 8.");
58 $sites = $this->setUpDrupal(1, TRUE);
61 'root' => $this->webroot(),
65 // Copy the 'woot' module over to the Drupal site we just set up.
66 $this->setupModulesForTests($this->webroot());
68 // Enable woot module, which contains a queue worker that throws a
70 $this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
72 // Add an item to the queue.
73 $this->drush('php-script', array('requeue_script'), $options + array('script-path' => dirname(__FILE__) . '/resources'));
75 // Check that the queue exists and it has one item in it.
76 $expected = 'woot_requeue_exception,%items,Drupal\Core\Queue\DatabaseQueue';
77 $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
78 $output = trim($this->getOutput());
79 $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
82 $this->drush('queue-run', array('woot_requeue_exception'), $options);
84 // Check that the item was processed after being requeued once.
85 // Here is the detailed workflow of what the above command did.
86 // 1. Drush calls drush queue-run woot_requeue_exception.
87 // 2. Drush claims the item. The worker sets a state variable (see below)
88 // and throws the RequeueException.
89 // 3. Drush catches the exception and puts it back in the queue.
90 // 4. Drush claims the next item, which is the one that we just requeued.
91 // 5. The worker finds the state variable, so it does not throw the
92 // RequeueException this time (see below).
93 // 6. Drush removes the item from the queue.
94 // 7. Command finishes. The queue is empty.
95 $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
96 $output = trim($this->getOutput());
97 $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue item processed after being requeued.');
101 * Copies the woot module into Drupal.
103 * @param string $root
104 * The path to the root directory of Drupal.
106 public function setupModulesForTests($root) {
107 $wootModule = __DIR__ . '/resources/modules/d' . UNISH_DRUPAL_MAJOR_VERSION . '/woot';
108 $modulesDir = "$root/sites/all/modules";
109 $this->mkdir($modulesDir);
110 \symlink($wootModule, "$modulesDir/woot");