3 namespace Drupal\devel_generate\Tests;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Language\Language;
9 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
10 use Drupal\node\Entity\Node;
11 use Drupal\simpletest\WebTestBase;
14 * Tests the logic to generate data.
16 * @group devel_generate
18 class DevelGenerateTest extends WebTestBase {
21 use EntityReferenceTestTrait;
24 * Vocabulary for testing.
26 * @var \Drupal\taxonomy\VocabularyInterface
28 protected $vocabulary;
35 public static $modules = array('menu_ui', 'node', 'comment', 'taxonomy', 'path', 'devel_generate');
38 * Prepares the testing environment
40 public function setUp() {
43 // Create Basic page and Article node types.
44 if ($this->profile != 'standard') {
45 $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic Page'));
46 $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
47 $this->addDefaultCommentField('node', 'article');
50 // Creating a vocabulary to associate taxonomy terms generated.
51 $this->vocabulary = entity_create('taxonomy_vocabulary', array(
52 'name' => $this->randomMachineName(),
53 'description' => $this->randomMachineName(),
54 'vid' => Unicode::strtolower($this->randomMachineName()),
55 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
56 'weight' => mt_rand(0, 10),
58 $this->vocabulary->save();
60 // Creates a field of an entity reference field storage on article.
61 $field_name = 'taxonomy_' . $this->vocabulary->id();
63 $handler_settings = array(
64 'target_bundles' => array(
65 $this->vocabulary->id() => $this->vocabulary->id(),
67 'auto_create' => TRUE,
69 $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
71 entity_get_form_display('node', 'article', 'default')
72 ->setComponent($field_name, array(
73 'type' => 'options_select',
77 entity_get_display('node', 'article', 'default')
78 ->setComponent($field_name, array(
79 'type' => 'entity_reference_label',
83 $admin_user = $this->drupalCreateUser(array('administer devel_generate'));
84 $this->drupalLogin($admin_user);
88 * Tests generate commands
90 public function testDevelGenerate() {
95 $this->drupalPostForm('admin/config/development/generate/user', $edit, t('Generate'));
96 $this->assertText(t('4 users created.'));
97 $this->assertText(t('Generate process complete.'));
99 // Tests that if no content types are selected an error message is shown.
104 $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
105 $this->assertText(t('Please select at least one content type'));
108 // First we create a node in order to test the Delete content checkbox.
109 $this->drupalCreateNode(array('type' => 'article'));
114 'node_types[article]' => TRUE,
115 'time_range' => 604800,
120 $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
121 $this->assertText(t('Deleted 1 nodes.'));
122 $this->assertText(t('Finished creating 4 nodes'));
123 $this->assertText(t('Generate process complete.'));
125 // Tests that nodes have been created in the generation process.
126 $nodes = Node::loadMultiple();
127 $this->assert(count($nodes) == 4, 'Nodes generated successfully.');
129 // Tests url alias for the generated nodes.
130 foreach ($nodes as $node) {
131 $alias = 'node-' . $node->id() . '-' . $node->bundle();
132 $this->drupalGet($alias);
133 $this->assertResponse('200');
134 $this->assertText($node->getTitle(), 'Generated url alias for the node works.');
139 'vids[]' => $this->vocabulary->id(),
141 'title_length' => 12,
143 $this->drupalPostForm('admin/config/development/generate/term', $edit, t('Generate'));
144 $this->assertText(t('Created the following new terms: '));
145 $this->assertText(t('Generate process complete.'));
147 // Creating vocabularies.
150 'title_length' => 12,
153 $this->drupalPostForm('admin/config/development/generate/vocabs', $edit, t('Generate'));
154 $this->assertText(t('Created the following new vocabularies: '));
155 $this->assertText(t('Generate process complete.'));
161 'title_length' => 12,
162 'link_types[node]' => 1,
163 'link_types[front]' => 1,
164 'link_types[external]' => 1,
169 $this->drupalPostForm('admin/config/development/generate/menu', $edit, t('Generate'));
170 $this->assertText(t('Created the following new menus: '));
171 $this->assertText(t('Created 7 new menu links'));
172 $this->assertText(t('Generate process complete.'));