DevelGenerateTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Drupal\devel_generate\Tests;
  3. use Drupal\comment\Tests\CommentTestTrait;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  6. use Drupal\Core\Language\Language;
  7. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  8. use Drupal\node\Entity\Node;
  9. use Drupal\simpletest\WebTestBase;
  10. /**
  11. * Tests the logic to generate data.
  12. *
  13. * @group devel_generate
  14. */
  15. class DevelGenerateTest extends WebTestBase {
  16. use CommentTestTrait;
  17. use EntityReferenceTestTrait;
  18. /**
  19. * Vocabulary for testing.
  20. *
  21. * @var \Drupal\taxonomy\VocabularyInterface
  22. */
  23. protected $vocabulary;
  24. /**
  25. * Modules to enable.
  26. *
  27. * @var array
  28. */
  29. public static $modules = array('menu_ui', 'node', 'comment', 'taxonomy', 'path', 'devel_generate');
  30. /**
  31. * Prepares the testing environment
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. // Create Basic page and Article node types.
  36. if ($this->profile != 'standard') {
  37. $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic Page'));
  38. $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
  39. $this->addDefaultCommentField('node', 'article');
  40. }
  41. // Creating a vocabulary to associate taxonomy terms generated.
  42. $this->vocabulary = entity_create('taxonomy_vocabulary', array(
  43. 'name' => $this->randomMachineName(),
  44. 'description' => $this->randomMachineName(),
  45. 'vid' => Unicode::strtolower($this->randomMachineName()),
  46. 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
  47. 'weight' => mt_rand(0, 10),
  48. ));
  49. $this->vocabulary->save();
  50. // Creates a field of an entity reference field storage on article.
  51. $field_name = 'taxonomy_' . $this->vocabulary->id();
  52. $handler_settings = array(
  53. 'target_bundles' => array(
  54. $this->vocabulary->id() => $this->vocabulary->id(),
  55. ),
  56. 'auto_create' => TRUE,
  57. );
  58. $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  59. entity_get_form_display('node', 'article', 'default')
  60. ->setComponent($field_name, array(
  61. 'type' => 'options_select',
  62. ))
  63. ->save();
  64. entity_get_display('node', 'article', 'default')
  65. ->setComponent($field_name, array(
  66. 'type' => 'entity_reference_label',
  67. ))
  68. ->save();
  69. $admin_user = $this->drupalCreateUser(array('administer devel_generate'));
  70. $this->drupalLogin($admin_user);
  71. }
  72. /**
  73. * Tests generate commands
  74. */
  75. public function testDevelGenerate() {
  76. // Creating users.
  77. $edit = array(
  78. 'num' => 4,
  79. );
  80. $this->drupalPostForm('admin/config/development/generate/user', $edit, t('Generate'));
  81. $this->assertText(t('4 users created.'));
  82. $this->assertText(t('Generate process complete.'));
  83. // Tests that if no content types are selected an error message is shown.
  84. $edit = array(
  85. 'num' => 4,
  86. 'title_length' => 4,
  87. );
  88. $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
  89. $this->assertText(t('Please select at least one content type'));
  90. // Creating content.
  91. // First we create a node in order to test the Delete content checkbox.
  92. $this->drupalCreateNode(array('type' => 'article'));
  93. $edit = array(
  94. 'num' => 4,
  95. 'kill' => TRUE,
  96. 'node_types[article]' => TRUE,
  97. 'time_range' => 604800,
  98. 'max_comments' => 3,
  99. 'title_length' => 4,
  100. 'add_alias' => 1,
  101. );
  102. $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
  103. $this->assertText(t('Deleted 1 nodes.'));
  104. $this->assertText(t('Finished creating 4 nodes'));
  105. $this->assertText(t('Generate process complete.'));
  106. // Tests that nodes have been created in the generation process.
  107. $nodes = Node::loadMultiple();
  108. $this->assert(count($nodes) == 4, 'Nodes generated successfully.');
  109. // Tests url alias for the generated nodes.
  110. foreach ($nodes as $node) {
  111. $alias = 'node-' . $node->id() . '-' . $node->bundle();
  112. $this->drupalGet($alias);
  113. $this->assertResponse('200');
  114. $this->assertText($node->getTitle(), 'Generated url alias for the node works.');
  115. }
  116. // Creating terms.
  117. $edit = array(
  118. 'vids[]' => $this->vocabulary->id(),
  119. 'num' => 5,
  120. 'title_length' => 12,
  121. );
  122. $this->drupalPostForm('admin/config/development/generate/term', $edit, t('Generate'));
  123. $this->assertText(t('Created the following new terms: '));
  124. $this->assertText(t('Generate process complete.'));
  125. // Creating vocabularies.
  126. $edit = array(
  127. 'num' => 5,
  128. 'title_length' => 12,
  129. 'kill' => TRUE,
  130. );
  131. $this->drupalPostForm('admin/config/development/generate/vocabs', $edit, t('Generate'));
  132. $this->assertText(t('Created the following new vocabularies: '));
  133. $this->assertText(t('Generate process complete.'));
  134. // Creating menus.
  135. $edit = array(
  136. 'num_menus' => 5,
  137. 'num_links' => 7,
  138. 'title_length' => 12,
  139. 'link_types[node]' => 1,
  140. 'link_types[front]' => 1,
  141. 'link_types[external]' => 1,
  142. 'max_depth' => 4,
  143. 'max_width' => 6,
  144. 'kill' => 1,
  145. );
  146. $this->drupalPostForm('admin/config/development/generate/menu', $edit, t('Generate'));
  147. $this->assertText(t('Created the following new menus: '));
  148. $this->assertText(t('Created 7 new menu links'));
  149. $this->assertText(t('Generate process complete.'));
  150. }
  151. }