3 namespace Drupal\pathauto\Tests;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\pathauto\Entity\PathautoPattern;
10 * Test basic pathauto functionality.
14 class PathautoUiTest extends WebTestBase {
16 use PathautoTestHelperTrait;
23 public static $modules = array('pathauto', 'node');
28 * @var \Drupal\user\UserInterface
38 $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
39 $this->drupalCreateContentType(array('type' => 'article'));
41 // Allow other modules to add additional permissions for the admin user.
43 'administer pathauto',
44 'administer url aliases',
48 'access content overview',
50 $this->adminUser = $this->drupalCreateUser($permissions);
51 $this->drupalLogin($this->adminUser);
54 function testSettingsValidation() {
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.');
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.');
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.');
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.');
83 function testPatternsWorkflow() {
84 // Try to save an empty pattern, should not be allowed.
85 $this->drupalGet('admin/config/search/path/patterns/add');
87 'type' => 'canonical_entities:node',
89 $this->drupalPostAjaxForm(NULL, $edit, 'type');
91 'bundles[page]' => TRUE,
92 'label' => 'Page pattern',
93 'id' => 'page_pattern',
95 $this->drupalPostForm(NULL, $edit, 'Save');
96 $this->assertText('Path pattern field is required.');
97 $this->assertNoText('The configuration options have been saved.');
99 // Try to save an invalid pattern.
101 'pattern' => '[node:title]/[user:name]/[term:name]',
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.');
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.');
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.');
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.');
123 \Drupal::service('pathauto.generator')->resetCaches();
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);
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'));
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');
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.');
157 // Load the pattern from storage and check if its disabled.
158 $pattern = PathautoPattern::load('page_pattern');
159 $this->assertFalse($pattern->status());
161 \Drupal::service('pathauto.generator')->resetCaches();
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);
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.');
176 // Reload pattern from storage and check if its enabled.
177 $pattern = PathautoPattern::load('page_pattern');
178 $this->assertTrue($pattern->status());
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.');
188 $this->assertFalse(PathautoPattern::load('page_pattern'));