Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / tests / QueueTest.php
1 <?php
2
3 namespace Unish;
4
5 use Webmozart\PathUtil\Path;
6
7 /**
8  * @group commands
9  */
10 class QueueCase extends CommandUnishTestCase
11 {
12     use TestModuleHelperTrait;
13
14     public function testQueue()
15     {
16         $expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
17         $sites = $this->setUpDrupal(1, true);
18
19         // Enable aggregator since it declares a queue.
20         $this->drush('pm-enable', ['aggregator']);
21
22         $this->drush('queue-list');
23         $output = $this->getOutput();
24         $this->assertContains('aggregator_feeds', $output, 'Queue list shows the declared queue.');
25
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.');
32
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.');
37     }
38
39   /**
40    * Tests the queue-delete command.
41    */
42     public function testQueueDelete()
43     {
44         $expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
45
46         $sites = $this->setUpDrupal(1, true);
47
48         // Enable aggregator since it declares a queue.
49         $this->drush('pm-enable', ['aggregator']);
50
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.');
56
57         $this->drush('queue-delete', ['aggregator_feeds']);
58
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.');
62     }
63
64   /**
65    * Tests the RequeueException.
66    */
67     public function testRequeueException()
68     {
69         $sites = $this->setUpDrupal(1, true);
70
71         // Copy the 'woot' module over to the Drupal site we just set up.
72         $this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
73
74         // Enable woot module, which contains a queue worker that throws a
75         // RequeueException.
76         $this->drush('pm-enable', ['woot'], [], null, null, self::EXIT_SUCCESS);
77
78         // Add an item to the queue.
79         $this->drush('php-script', ['requeue_script'], ['script-path' => __DIR__ . '/resources']);
80
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.');
86
87         // Process the queue.
88         $this->drush('queue-run', ['woot_requeue_exception']);
89
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.');
104     }
105 }