5 use Webmozart\PathUtil\Path;
10 class QueueCase extends CommandUnishTestCase
12 use TestModuleHelperTrait;
14 public function testQueue()
16 $expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
17 $sites = $this->setUpDrupal(1, true);
19 // Enable aggregator since it declares a queue.
20 $this->drush('pm-enable', ['aggregator']);
22 $this->drush('queue-list');
23 $output = $this->getOutput();
24 $this->assertContains('aggregator_feeds', $output, 'Queue list shows the declared queue.');
26 // We need user to own to the feed.
27 $this->drush('user-create', ['example'], ['password' => 'password', 'mail' => "example@example.com"]);
28 $this->drush('php-script', ['queue_script'], ['script-path' => __DIR__ . '/resources']);
29 $this->drush('queue-list', [], ['format' => 'csv']);
30 $output = $this->getOutputAsList();
31 $this->assertEquals(str_replace('%items', 1, $expected), array_pop($output), 'Item was successfully added to the queue.');
33 $this->drush('queue-run', ['aggregator_feeds']);
34 $this->drush('queue-list', [], ['format' => 'csv']);
35 $output = $this->getOutputAsList();
36 $this->assertEquals(str_replace('%items', 0, $expected), array_pop($output), 'Queue item processed.');
40 * Tests the queue-delete command.
42 public function testQueueDelete()
44 $expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
46 $sites = $this->setUpDrupal(1, true);
48 // Enable aggregator since it declares a queue.
49 $this->drush('pm-enable', ['aggregator']);
51 // Add another item to the queue and make sure it was deleted.
52 $this->drush('php-script', ['queue_script'], ['script-path' => __DIR__ . '/resources']);
53 $this->drush('queue-list', [], ['format' => 'csv']);
54 $output = $this->getOutputAsList();
55 $this->assertEquals(str_replace('%items', 1, $expected), array_pop($output), 'Item was successfully added to the queue.');
57 $this->drush('queue-delete', ['aggregator_feeds']);
59 $this->drush('queue-list', [], ['format' => 'csv']);
60 $output = $this->getOutputAsList();
61 $this->assertEquals(str_replace('%items', 0, $expected), array_pop($output), 'Queue was successfully deleted.');
65 * Tests the RequeueException.
67 public function testRequeueException()
69 $sites = $this->setUpDrupal(1, true);
71 // Copy the 'woot' module over to the Drupal site we just set up.
72 $this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
74 // Enable woot module, which contains a queue worker that throws a
76 $this->drush('pm-enable', ['woot'], [], null, null, self::EXIT_SUCCESS);
78 // Add an item to the queue.
79 $this->drush('php-script', ['requeue_script'], ['script-path' => __DIR__ . '/resources']);
81 // Check that the queue exists and it has one item in it.
82 $expected = 'woot_requeue_exception,%items,"Drupal\Core\Queue\DatabaseQueue"';
83 $this->drush('queue-list', [], ['format' => 'csv']);
84 $output = $this->getOutputAsList();
85 $this->assertEquals(str_replace('%items', 1, $expected), array_pop($output), 'Item was successfully added to the queue.');
88 $this->drush('queue-run', ['woot_requeue_exception']);
90 // Check that the item was processed after being requeued once.
91 // Here is the detailed workflow of what the above command did.
92 // 1. Drush calls drush queue-run woot_requeue_exception.
93 // 2. Drush claims the item. The worker sets a state variable (see below)
94 // and throws the RequeueException.
95 // 3. Drush catches the exception and puts it back in the queue.
96 // 4. Drush claims the next item, which is the one that we just requeued.
97 // 5. The worker finds the state variable, so it does not throw the
98 // RequeueException this time (see below).
99 // 6. Drush removes the item from the queue.
100 // 7. Command finishes. The queue is empty.
101 $this->drush('queue-list', [], ['format' => 'csv']);
102 $output = $this->getOutputAsList();
103 $this->assertEquals(str_replace('%items', 0, $expected), array_pop($output), 'Queue item processed after being requeued.');