Version 1
[yaffs-website] / web / modules / contrib / devel / devel_generate / src / Tests / DevelGenerateTest.php
1 <?php
2
3 namespace Drupal\devel_generate\Tests;
4
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;
12
13 /**
14  * Tests the logic to generate data.
15  *
16  * @group devel_generate
17  */
18 class DevelGenerateTest extends WebTestBase {
19
20   use CommentTestTrait;
21   use EntityReferenceTestTrait;
22
23   /**
24    * Vocabulary for testing.
25    *
26    * @var \Drupal\taxonomy\VocabularyInterface
27    */
28   protected $vocabulary;
29
30   /**
31    * Modules to enable.
32    *
33    * @var array
34    */
35   public static $modules = array('menu_ui', 'node', 'comment', 'taxonomy', 'path', 'devel_generate');
36
37   /**
38    * Prepares the testing environment
39    */
40   public function setUp() {
41     parent::setUp();
42
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');
48     }
49
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),
57     ));
58     $this->vocabulary->save();
59
60     // Creates a field of an entity reference field storage on article.
61     $field_name = 'taxonomy_' . $this->vocabulary->id();
62
63     $handler_settings = array(
64       'target_bundles' => array(
65         $this->vocabulary->id() => $this->vocabulary->id(),
66       ),
67       'auto_create' => TRUE,
68     );
69     $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
70
71     entity_get_form_display('node', 'article', 'default')
72       ->setComponent($field_name, array(
73         'type' => 'options_select',
74       ))
75       ->save();
76
77     entity_get_display('node', 'article', 'default')
78       ->setComponent($field_name, array(
79         'type' => 'entity_reference_label',
80       ))
81       ->save();
82
83     $admin_user = $this->drupalCreateUser(array('administer devel_generate'));
84     $this->drupalLogin($admin_user);
85   }
86
87   /**
88    * Tests generate commands
89    */
90   public function testDevelGenerate() {
91     // Creating users.
92     $edit = array(
93       'num' => 4,
94     );
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.'));
98
99     // Tests that if no content types are selected an error message is shown.
100     $edit = array(
101       'num' => 4,
102       'title_length' => 4,
103     );
104     $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
105     $this->assertText(t('Please select at least one content type'));
106
107     // Creating content.
108     // First we create a node in order to test the Delete content checkbox.
109     $this->drupalCreateNode(array('type' => 'article'));
110
111     $edit = array(
112       'num' => 4,
113       'kill' => TRUE,
114       'node_types[article]' => TRUE,
115       'time_range' => 604800,
116       'max_comments' => 3,
117       'title_length' => 4,
118       'add_alias' => 1,
119     );
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.'));
124
125     // Tests that nodes have been created in the generation process.
126     $nodes = Node::loadMultiple();
127     $this->assert(count($nodes) == 4, 'Nodes generated successfully.');
128
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.');
135     }
136
137     // Creating terms.
138     $edit = array(
139       'vids[]' => $this->vocabulary->id(),
140       'num' => 5,
141       'title_length' => 12,
142     );
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.'));
146
147     // Creating vocabularies.
148     $edit = array(
149       'num' => 5,
150       'title_length' => 12,
151       'kill' => TRUE,
152     );
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.'));
156
157     // Creating menus.
158     $edit = array(
159       'num_menus' => 5,
160       'num_links' => 7,
161       'title_length' => 12,
162       'link_types[node]' => 1,
163       'link_types[front]' => 1,
164       'link_types[external]' => 1,
165       'max_depth' => 4,
166       'max_width' => 6,
167       'kill' => 1,
168     );
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.'));
173   }
174
175 }