Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoUiTest.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\pathauto\Entity\PathautoPattern;
8
9 /**
10  * Test basic pathauto functionality.
11  *
12  * @group pathauto
13  */
14 class PathautoUiTest extends WebTestBase {
15
16   use PathautoTestHelperTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = array('pathauto', 'node');
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * {@inheritdoc}
34    */
35   function setUp() {
36     parent::setUp();
37
38     $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
39     $this->drupalCreateContentType(array('type' => 'article'));
40
41     // Allow other modules to add additional permissions for the admin user.
42     $permissions = array(
43       'administer pathauto',
44       'administer url aliases',
45       'create url aliases',
46       'administer nodes',
47       'bypass node access',
48       'access content overview',
49     );
50     $this->adminUser = $this->drupalCreateUser($permissions);
51     $this->drupalLogin($this->adminUser);
52   }
53
54   function testSettingsValidation() {
55     $edit = array();
56     $edit['max_length'] = 'abc';
57     $edit['max_component_length'] = 'abc';
58     $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
59     /*$this->assertText('The field Maximum alias length is not a valid number.');
60     $this->assertText('The field Maximum component length is not a valid number.');*/
61     $this->assertNoText('The configuration options have been saved.');
62
63     $edit['max_length'] = '0';
64     $edit['max_component_length'] = '0';
65     $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
66     /*$this->assertText('The field Maximum alias length cannot be less than 1.');
67     $this->assertText('The field Maximum component length cannot be less than 1.');*/
68     $this->assertNoText('The configuration options have been saved.');
69
70     $edit['max_length'] = '999';
71     $edit['max_component_length'] = '999';
72     $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
73     /*$this->assertText('The field Maximum alias length cannot be greater than 255.');
74     $this->assertText('The field Maximum component length cannot be greater than 255.');*/
75     $this->assertNoText('The configuration options have been saved.');
76
77     $edit['max_length'] = '50';
78     $edit['max_component_length'] = '50';
79     $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
80     $this->assertText('The configuration options have been saved.');
81   }
82
83   function testPatternsWorkflow() {
84     // Try to save an empty pattern, should not be allowed.
85     $this->drupalGet('admin/config/search/path/patterns/add');
86     $edit = array(
87       'type' => 'canonical_entities:node',
88     );
89     $this->drupalPostAjaxForm(NULL, $edit, 'type');
90     $edit += array(
91       'bundles[page]' => TRUE,
92       'label' => 'Page pattern',
93       'id' => 'page_pattern',
94     );
95     $this->drupalPostForm(NULL, $edit, 'Save');
96     $this->assertText('Path pattern field is required.');
97     $this->assertNoText('The configuration options have been saved.');
98
99     // Try to save an invalid pattern.
100     $edit += array(
101       'pattern' => '[node:title]/[user:name]/[term:name]',
102     );
103     $this->drupalPostForm(NULL, $edit, 'Save');
104     $this->assertText('Path pattern is using the following invalid tokens: [user:name], [term:name].');
105     $this->assertNoText('The configuration options have been saved.');
106
107     $edit['pattern'] = '#[node:title]';
108     $this->drupalPostForm(NULL, $edit, 'Save');
109     $this->assertText('The Path pattern is using the following invalid characters: #.');
110     $this->assertNoText('The configuration options have been saved.');
111
112     // Checking whitespace ending of the string.
113     $edit['pattern'] = '[node:title] ';
114     $this->drupalPostForm(NULL, $edit, 'Save');
115     $this->assertText('The Path pattern doesn\'t allow the patterns ending with whitespace.');
116     $this->assertNoText('The configuration options have been saved.');
117
118     // Fix the pattern, then check that it gets saved successfully.
119     $edit['pattern'] = '[node:title]';
120     $this->drupalPostForm(NULL, $edit, 'Save');
121     $this->assertText('Pattern Page pattern saved.');
122
123     \Drupal::service('pathauto.generator')->resetCaches();
124
125     // Create a node with pattern enabled and check if the pattern applies.
126     $title = 'Page Pattern enabled';
127     $alias = '/page-pattern-enabled';
128     $node = $this->createNode(['title' => $title, 'type' => 'page']);
129     $this->drupalGet($alias);
130     $this->assertResponse(200);
131     $this->assertEntityAlias($node, $alias);
132
133     // Edit workflow, set a new label and weight for the pattern.
134     $this->drupalPostForm('/admin/config/search/path/patterns', ['entities[page_pattern][weight]' => '4'], t('Save'));
135     $this->clickLink(t('Edit'));
136     $destination_query = ['query' => ['destination' => Url::fromRoute('entity.pathauto_pattern.collection')->toString()]];
137     $this->assertUrl('/admin/config/search/path/patterns/page_pattern', $destination_query);
138     $this->assertFieldByName('pattern', '[node:title]');
139     $this->assertFieldByName('label', 'Page pattern');
140     $this->assertFieldChecked('edit-status');
141     $this->assertLink(t('Delete'));
142
143     $edit = array('label' => 'Test');
144     $this->drupalPostForm('/admin/config/search/path/patterns/page_pattern', $edit, t('Save'));
145     $this->assertText('Pattern Test saved.');
146     // Check that the pattern weight did not change.
147     $this->assertOptionSelected('edit-entities-page-pattern-weight', '4');
148
149     // Disable workflow.
150     $this->drupalGet('/admin/config/search/path/patterns');
151     $this->assertNoLink(t('Enable'));
152     $this->clickLink(t('Disable'));
153     $this->assertUrl('/admin/config/search/path/patterns/page_pattern/disable', $destination_query);
154     $this->drupalPostForm(NULL, [], t('Disable'));
155     $this->assertText('Disabled pattern Test.');
156
157     // Load the pattern from storage and check if its disabled.
158     $pattern = PathautoPattern::load('page_pattern');
159     $this->assertFalse($pattern->status());
160
161     \Drupal::service('pathauto.generator')->resetCaches();
162
163     // Create a node with pattern disabled and check that we have no new alias.
164     $title = 'Page Pattern disabled';
165     $node = $this->createNode(['title' => $title, 'type' => 'page']);
166     $this->assertNoEntityAlias($node);
167
168     // Enable workflow.
169     $this->drupalGet('/admin/config/search/path/patterns');
170     $this->assertNoLink(t('Disable'));
171     $this->clickLink(t('Enable'));
172     $this->assertUrl('/admin/config/search/path/patterns/page_pattern/enable', $destination_query);
173     $this->drupalPostForm(NULL, [], t('Enable'));
174     $this->assertText('Enabled pattern Test.');
175
176     // Reload pattern from storage and check if its enabled.
177     $pattern = PathautoPattern::load('page_pattern');
178     $this->assertTrue($pattern->status());
179
180     // Delete workflow.
181     $this->drupalGet('/admin/config/search/path/patterns');
182     $this->clickLink(t('Delete'));
183     $this->assertUrl('/admin/config/search/path/patterns/page_pattern/delete', $destination_query);
184     $this->assertText(t('This action cannot be undone.'));
185     $this->drupalPostForm(NULL, [], t('Delete'));
186     $this->assertText('The pathauto pattern Test has been deleted.');
187
188     $this->assertFalse(PathautoPattern::load('page_pattern'));
189   }
190
191 }