3 namespace Drupal\pathauto\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\pathauto\Entity\PathautoPattern;
9 * Test basic pathauto functionality.
13 class PathautoUiTest extends WebTestBase {
15 use PathautoTestHelperTrait;
22 public static $modules = array('pathauto', 'node');
27 * @var \Drupal\user\UserInterface
37 $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
38 $this->drupalCreateContentType(array('type' => 'article'));
40 // Allow other modules to add additional permissions for the admin user.
42 'administer pathauto',
43 'administer url aliases',
47 'access content overview',
49 $this->adminUser = $this->drupalCreateUser($permissions);
50 $this->drupalLogin($this->adminUser);
53 function testSettingsValidation() {
55 $edit['max_length'] = 'abc';
56 $edit['max_component_length'] = 'abc';
57 $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
58 /*$this->assertText('The field Maximum alias length is not a valid number.');
59 $this->assertText('The field Maximum component length is not a valid number.');*/
60 $this->assertNoText('The configuration options have been saved.');
62 $edit['max_length'] = '0';
63 $edit['max_component_length'] = '0';
64 $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
65 /*$this->assertText('The field Maximum alias length cannot be less than 1.');
66 $this->assertText('The field Maximum component length cannot be less than 1.');*/
67 $this->assertNoText('The configuration options have been saved.');
69 $edit['max_length'] = '999';
70 $edit['max_component_length'] = '999';
71 $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
72 /*$this->assertText('The field Maximum alias length cannot be greater than 255.');
73 $this->assertText('The field Maximum component length cannot be greater than 255.');*/
74 $this->assertNoText('The configuration options have been saved.');
76 $edit['max_length'] = '50';
77 $edit['max_component_length'] = '50';
78 $this->drupalPostForm('admin/config/search/path/settings', $edit, 'Save configuration');
79 $this->assertText('The configuration options have been saved.');
82 function testPatternsWorkflow() {
83 // Try to save an empty pattern, should not be allowed.
84 $this->drupalGet('admin/config/search/path/patterns/add');
86 'type' => 'canonical_entities:node',
88 $this->drupalPostAjaxForm(NULL, $edit, 'type');
90 'bundles[page]' => TRUE,
91 'label' => 'Page pattern',
92 'id' => 'page_pattern',
94 $this->drupalPostForm(NULL, $edit, 'Save');
95 $this->assertText('Path pattern field is required.');
96 $this->assertNoText('The configuration options have been saved.');
98 // Try to save an invalid pattern.
100 'pattern' => '[node:title]/[user:name]/[term:name]',
102 $this->drupalPostForm(NULL, $edit, 'Save');
103 $this->assertText('Path pattern is using the following invalid tokens: [user:name], [term:name].');
104 $this->assertNoText('The configuration options have been saved.');
106 $edit['pattern'] = '#[node:title]';
107 $this->drupalPostForm(NULL, $edit, 'Save');
108 $this->assertText('The Path pattern is using the following invalid characters: #.');
109 $this->assertNoText('The configuration options have been saved.');
111 // Checking whitespace ending of the string.
112 $edit['pattern'] = '[node:title] ';
113 $this->drupalPostForm(NULL, $edit, 'Save');
114 $this->assertText('The Path pattern doesn\'t allow the patterns ending with whitespace.');
115 $this->assertNoText('The configuration options have been saved.');
117 // Fix the pattern, then check that it gets saved successfully.
118 $edit['pattern'] = '[node:title]';
119 $this->drupalPostForm(NULL, $edit, 'Save');
120 $this->assertText('Pattern Page pattern saved.');
122 \Drupal::service('pathauto.generator')->resetCaches();
124 // Create a node with pattern enabled and check if the pattern applies.
125 $title = 'Page Pattern enabled';
126 $alias = '/page-pattern-enabled';
127 $node = $this->createNode(['title' => $title, 'type' => 'page']);
128 $this->drupalGet($alias);
129 $this->assertResponse(200);
130 $this->assertEntityAlias($node, $alias);
132 // Edit workflow, set a new label and weight for the pattern.
133 $this->drupalPostForm('/admin/config/search/path/patterns', ['entities[page_pattern][weight]' => '4'], t('Save'));
134 $this->clickLink(t('Edit'));
135 $this->assertUrl('/admin/config/search/path/patterns/page_pattern');
136 $this->assertFieldByName('pattern', '[node:title]');
137 $this->assertFieldByName('label', 'Page pattern');
138 $this->assertFieldChecked('edit-status');
139 $this->assertLink(t('Delete'));
141 $edit = array('label' => 'Test');
142 $this->drupalPostForm('/admin/config/search/path/patterns/page_pattern', $edit, t('Save'));
143 $this->assertText('Pattern Test saved.');
144 // Check that the pattern weight did not change.
145 $this->assertOptionSelected('edit-entities-page-pattern-weight', '4');
148 $this->drupalGet('/admin/config/search/path/patterns');
149 $this->assertNoLink(t('Enable'));
150 $this->clickLink(t('Disable'));
151 $this->assertUrl('/admin/config/search/path/patterns/page_pattern/disable');
152 $this->drupalPostForm(NULL, [], t('Disable'));
153 $this->assertText('Disabled pattern Test.');
155 // Load the pattern from storage and check if its disabled.
156 $pattern = PathautoPattern::load('page_pattern');
157 $this->assertFalse($pattern->status());
159 \Drupal::service('pathauto.generator')->resetCaches();
161 // Create a node with pattern disabled and check that we have no new alias.
162 $title = 'Page Pattern disabled';
163 $node = $this->createNode(['title' => $title, 'type' => 'page']);
164 $this->assertNoEntityAlias($node);
167 $this->drupalGet('/admin/config/search/path/patterns');
168 $this->assertNoLink(t('Disable'));
169 $this->clickLink(t('Enable'));
170 $this->assertUrl('/admin/config/search/path/patterns/page_pattern/enable');
171 $this->drupalPostForm(NULL, [], t('Enable'));
172 $this->assertText('Enabled pattern Test.');
174 // Reload pattern from storage and check if its enabled.
175 $pattern = PathautoPattern::load('page_pattern');
176 $this->assertTrue($pattern->status());
179 $this->drupalGet('/admin/config/search/path/patterns');
180 $this->clickLink(t('Delete'));
181 $this->assertUrl('/admin/config/search/path/patterns/page_pattern/delete');
182 $this->assertText(t('This action cannot be undone.'));
183 $this->drupalPostForm(NULL, [], t('Delete'));
184 $this->assertText('The pathauto pattern Test has been deleted.');
186 $this->assertFalse(PathautoPattern::load('page_pattern'));