5 if (class_exists('Unish\CommandUnishTestCase')) {
8 * Tests for devel_generate drush commands.
10 * @group devel_generate
12 class DevelGenerateUnishTest extends CommandUnishTestCase {
17 public function setUp() {
18 if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
19 $this->markTestSkipped('Devel Generate Tests only available on D8+.');
22 if (!$this->getSites()) {
23 $this->setUpDrupal(1, TRUE, UNISH_DRUPAL_MAJOR_VERSION, 'standard');
25 // Symlink the devel module into the sandbox.
26 $devel_directory = dirname(dirname(__DIR__));
27 symlink($devel_directory, $this->webroot() . '/modules/devel');
29 // Enable the devel_generate modules.
30 $this->drush('pm-enable', ['devel_generate'], $this->getOptions());
36 * Tests devel generate terms.
38 public function testDevelGenerateTerms() {
39 $this->drush('pm-enable', ['taxonomy'], $this->getOptions());
41 $this->drush('generate-terms', [], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
42 $this->assertContains('Please provide a vocabulary machine name.', $this->getErrorOutput());
44 $this->drush('generate-terms', ['unknown'], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
45 $this->assertContains('Invalid vocabulary name: unknown', $this->getErrorOutput());
47 $this->drush('generate-terms', ['tags', 'NaN'], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
48 $this->assertContains('Invalid number of terms: NaN', $this->getErrorOutput());
50 $eval_term_count = "return \\Drupal::entityQuery('taxonomy_term')->count()->execute();";
51 $eval_options = $this->getOptions() + ['format' => 'string'];
53 $this->drush('generate-terms', ['tags'], $this->getOptions());
54 $this->assertContains('Created the following new terms:', $this->getErrorOutput());
55 $this->drush('php-eval', [$eval_term_count], $eval_options);
56 $this->assertEquals(10, $this->getOutput());
58 $this->drush('generate-terms', ['tags', '1'], $this->getOptions());
59 $this->assertContains('Created the following new terms:', $this->getErrorOutput());
60 $this->drush('php-eval', [$eval_term_count], $eval_options);
61 $this->assertEquals(11, $this->getOutput());
63 $this->drush('generate-terms', ['tags', '1'], $this->getOptions(TRUE));
64 $this->assertContains('Deleted existing terms.', $this->getErrorOutput());
65 $this->assertContains('Created the following new terms:', $this->getErrorOutput());
66 $this->drush('php-eval', [$eval_term_count], $eval_options);
67 $this->assertEquals(1, $this->getOutput());
69 $this->drush('gent', ['tags', '1'], $this->getOptions());
70 $this->assertContains('Created the following new terms:', $this->getErrorOutput());
74 * Tests devel generate contents.
76 public function testDevelGenerateContents() {
77 $this->drush('pm-enable', ['node'], $this->getOptions());
79 $eval_content_count = "return \\Drupal::entityQuery('node')->count()->execute();";
80 $eval_options = $this->getOptions() + ['format' => 'string'];
82 // Try to generate 10 content of type "page" or "article"
83 $this->drush('generate-content', [10], $this->getOptions(), NULL, NULL, static::EXIT_SUCCESS);
84 $this->assertContains('Finished creating 10 nodes', $this->getErrorOutput());
85 $this->drush('php-eval', [$eval_content_count], $eval_options);
86 $this->assertEquals(10, $this->getOutput());
88 // Try to generate 1 content of type "page" or "article"
89 $this->drush('generate-content', [1], $this->getOptions(), NULL, NULL, static::EXIT_SUCCESS);
90 $this->assertContains('1 node created.', $this->getErrorOutput());
91 $this->drush('php-eval', [$eval_content_count], $eval_options);
92 $this->assertEquals(11, $this->getOutput());
94 // Try to generate 5 content of type "page" or "article", removing all
96 $this->drush('generate-content', [5], $this->getOptions(TRUE), NULL, NULL, static::EXIT_SUCCESS);
97 $this->assertContains('Finished creating 5 nodes', $this->getErrorOutput());
98 $this->drush('php-eval', [$eval_content_count], $eval_options);
99 $this->assertEquals(5, $this->getOutput());
101 // Try to generate other 5 content with "crappy" type. Output should
103 $generate_content_wrong_ct = $this->getOptions(TRUE) + ['types' => 'crappy'];
104 $this->drush('generate-content', [5], $generate_content_wrong_ct, NULL, NULL, static::EXIT_ERROR);
105 $this->assertContains('One or more content types have been entered that don', $this->getErrorOutput());
106 $this->drush('php-eval', [$eval_content_count], $eval_options);
107 $this->assertEquals(5, $this->getOutput());
109 // Try to generate other 5 content with empty types. Output should
111 $generate_content_no_types = $this->getOptions(TRUE) + ['types' => ''];
112 $this->drush('generate-content', [5], $generate_content_no_types, NULL, NULL, static::EXIT_ERROR);
113 $this->assertContains('No content types available', $this->getErrorOutput());
114 $this->drush('php-eval', [$eval_content_count], $eval_options);
115 $this->assertEquals(5, $this->getOutput());
117 // Try to generate other 5 content without any types. Output should
119 $generate_content_no_types = $this->getOptions(TRUE) + ['types' => NULL];
120 $this->drush('generate-content', [5], $generate_content_no_types, NULL, NULL, static::EXIT_ERROR);
121 $this->assertContains('Wrong syntax or no content type selected. The correct syntax uses', $this->getErrorOutput());
122 $this->drush('php-eval', [$eval_content_count], $eval_options);
123 $this->assertEquals(5, $this->getOutput());
127 * Default drush options.
130 * Whether add kill option.
133 * An array containing the default options for drush commands.
135 protected function getOptions($kill = FALSE) {
138 'root' => $this->webroot(),
139 'uri' => key($this->getSites()),
143 $options['kill'] = NULL;