DevelGenerateUnishTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Unish;
  3. if (class_exists('Unish\CommandUnishTestCase')) {
  4. /**
  5. * Tests for devel_generate drush commands.
  6. *
  7. * @group devel_generate
  8. */
  9. class DevelGenerateUnishTest extends CommandUnishTestCase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function setUp() {
  14. if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
  15. $this->markTestSkipped('Devel Generate Tests only available on D8+.');
  16. }
  17. if (!$this->getSites()) {
  18. $this->setUpDrupal(1, TRUE, UNISH_DRUPAL_MAJOR_VERSION, 'standard');
  19. // Symlink the devel module into the sandbox.
  20. $devel_directory = dirname(dirname(__DIR__));
  21. symlink($devel_directory, $this->webroot() . '/modules/devel');
  22. // Enable the devel_generate modules.
  23. $this->drush('pm-enable', ['devel_generate'], $this->getOptions());
  24. }
  25. }
  26. /**
  27. * Tests devel generate terms.
  28. */
  29. public function testDevelGenerateTerms() {
  30. $this->drush('pm-enable', ['taxonomy'], $this->getOptions());
  31. $this->drush('generate-terms', [], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
  32. $this->assertContains('Please provide a vocabulary machine name.', $this->getErrorOutput());
  33. $this->drush('generate-terms', ['unknown'], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
  34. $this->assertContains('Invalid vocabulary name: unknown', $this->getErrorOutput());
  35. $this->drush('generate-terms', ['tags', 'NaN'], $this->getOptions(), NULL, NULL, static::EXIT_ERROR);
  36. $this->assertContains('Invalid number of terms: NaN', $this->getErrorOutput());
  37. $eval_term_count = "return \\Drupal::entityQuery('taxonomy_term')->count()->execute();";
  38. $eval_options = $this->getOptions() + ['format' => 'string'];
  39. $this->drush('generate-terms', ['tags'], $this->getOptions());
  40. $this->assertContains('Created the following new terms:', $this->getErrorOutput());
  41. $this->drush('php-eval', [$eval_term_count], $eval_options);
  42. $this->assertEquals(10, $this->getOutput());
  43. $this->drush('generate-terms', ['tags', '1'], $this->getOptions());
  44. $this->assertContains('Created the following new terms:', $this->getErrorOutput());
  45. $this->drush('php-eval', [$eval_term_count], $eval_options);
  46. $this->assertEquals(11, $this->getOutput());
  47. $this->drush('generate-terms', ['tags', '1'], $this->getOptions(TRUE));
  48. $this->assertContains('Deleted existing terms.', $this->getErrorOutput());
  49. $this->assertContains('Created the following new terms:', $this->getErrorOutput());
  50. $this->drush('php-eval', [$eval_term_count], $eval_options);
  51. $this->assertEquals(1, $this->getOutput());
  52. $this->drush('gent', ['tags', '1'], $this->getOptions());
  53. $this->assertContains('Created the following new terms:', $this->getErrorOutput());
  54. }
  55. /**
  56. * Tests devel generate contents.
  57. */
  58. public function testDevelGenerateContents() {
  59. $this->drush('pm-enable', ['node'], $this->getOptions());
  60. $eval_content_count = "return \\Drupal::entityQuery('node')->count()->execute();";
  61. $eval_options = $this->getOptions() + ['format' => 'string'];
  62. // Try to generate 10 content of type "page" or "article"
  63. $this->drush('generate-content', [10], $this->getOptions(), NULL, NULL, static::EXIT_SUCCESS);
  64. $this->assertContains('Finished creating 10 nodes', $this->getErrorOutput());
  65. $this->drush('php-eval', [$eval_content_count], $eval_options);
  66. $this->assertEquals(10, $this->getOutput());
  67. // Try to generate 1 content of type "page" or "article"
  68. $this->drush('generate-content', [1], $this->getOptions(), NULL, NULL, static::EXIT_SUCCESS);
  69. $this->assertContains('1 node created.', $this->getErrorOutput());
  70. $this->drush('php-eval', [$eval_content_count], $eval_options);
  71. $this->assertEquals(11, $this->getOutput());
  72. // Try to generate 5 content of type "page" or "article", removing all
  73. // previous contents.
  74. $this->drush('generate-content', [5], $this->getOptions(TRUE), NULL, NULL, static::EXIT_SUCCESS);
  75. $this->assertContains('Finished creating 5 nodes', $this->getErrorOutput());
  76. $this->drush('php-eval', [$eval_content_count], $eval_options);
  77. $this->assertEquals(5, $this->getOutput());
  78. // Try to generate other 5 content with "crappy" type. Output should
  79. // remains 5.
  80. $generate_content_wrong_ct = $this->getOptions(TRUE) + ['types' => 'crappy'];
  81. $this->drush('generate-content', [5], $generate_content_wrong_ct, NULL, NULL, static::EXIT_ERROR);
  82. $this->assertContains('One or more content types have been entered that don', $this->getErrorOutput());
  83. $this->drush('php-eval', [$eval_content_count], $eval_options);
  84. $this->assertEquals(5, $this->getOutput());
  85. // Try to generate other 5 content with empty types. Output should
  86. // remains 5.
  87. $generate_content_no_types = $this->getOptions(TRUE) + ['types' => ''];
  88. $this->drush('generate-content', [5], $generate_content_no_types, NULL, NULL, static::EXIT_ERROR);
  89. $this->assertContains('No content types available', $this->getErrorOutput());
  90. $this->drush('php-eval', [$eval_content_count], $eval_options);
  91. $this->assertEquals(5, $this->getOutput());
  92. // Try to generate other 5 content without any types. Output should
  93. // remains 5.
  94. $generate_content_no_types = $this->getOptions(TRUE) + ['types' => NULL];
  95. $this->drush('generate-content', [5], $generate_content_no_types, NULL, NULL, static::EXIT_ERROR);
  96. $this->assertContains('Wrong syntax or no content type selected. The correct syntax uses', $this->getErrorOutput());
  97. $this->drush('php-eval', [$eval_content_count], $eval_options);
  98. $this->assertEquals(5, $this->getOutput());
  99. }
  100. /**
  101. * Default drush options.
  102. *
  103. * @param bool $kill
  104. * Whether add kill option.
  105. *
  106. * @return array
  107. * An array containing the default options for drush commands.
  108. */
  109. protected function getOptions($kill = FALSE) {
  110. $options = [
  111. 'yes' => NULL,
  112. 'root' => $this->webroot(),
  113. 'uri' => key($this->getSites()),
  114. ];
  115. if($kill) {
  116. $options['kill'] = NULL;
  117. }
  118. return $options;
  119. }
  120. }
  121. }