Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / tests / queueTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * @group commands
7  */
8 class QueueCase extends CommandUnishTestCase {
9
10   function testQueue() {
11     if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
12       $this->markTestSkipped("Queue API not available in Drupal 6.");
13     }
14
15     if (UNISH_DRUPAL_MAJOR_VERSION == 7) {
16       $expected = 'aggregator_feeds,%items,SystemQueue';
17     }
18     else {
19       $expected = 'aggregator_feeds,%items,Drupal\Core\Queue\DatabaseQueue';
20     }
21
22     $sites = $this->setUpDrupal(1, TRUE);
23     $options = array(
24       'yes' => NULL,
25       'root' => $this->webroot(),
26       'uri' => key($sites),
27     );
28
29     // Enable aggregator since it declares a queue.
30     $this->drush('pm-enable', array('aggregator'), $options);
31
32     $this->drush('queue-list', array(), $options);
33     $output = $this->getOutput();
34     $this->assertContains('aggregator_feeds', $output, 'Queue list shows the declared queue.');
35
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();
42
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.');
48   }
49
50   /**
51    * Tests the RequeueException.
52    */
53   public function testRequeueException() {
54     if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
55       $this->markTestSkipped("RequeueException only available in Drupal 8.");
56     }
57
58     $sites = $this->setUpDrupal(1, TRUE);
59     $options = array(
60       'yes' => NULL,
61       'root' => $this->webroot(),
62       'uri' => key($sites),
63     );
64
65     // Copy the 'woot' module over to the Drupal site we just set up.
66     $this->setupModulesForTests($this->webroot());
67
68     // Enable woot module, which contains a queue worker that throws a
69     // RequeueException.
70     $this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
71
72     // Add an item to the queue.
73     $this->drush('php-script', array('requeue_script'), $options + array('script-path' => dirname(__FILE__) . '/resources'));
74
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.');
80
81     // Process the queue.
82     $this->drush('queue-run', array('woot_requeue_exception'), $options);
83
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.');
98   }
99
100   /**
101    * Copies the woot module into Drupal.
102    *
103    * @param string $root
104    *   The path to the root directory of Drupal.
105    */
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");
111   }
112
113 }