/[drupal]/drupal/modules/path/path.test
ViewVC logotype

Contents of /drupal/modules/path/path.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.25 - (show annotations) (download) (as text)
Tue Oct 20 01:24:34 2009 UTC (5 weeks, 1 day ago) by dries
Branch: MAIN
Changes since 1.24: +77 -22 lines
File MIME type: text/x-php
- Patch #332333 by sun: more fixes for the path API.
1 <?php
2 // $Id: path.test,v 1.24 2009/10/16 02:04:43 webchick Exp $
3
4 /**
5 * @file
6 * Tests for the path module
7 */
8
9 class PathTestCase extends DrupalWebTestCase {
10 public static function getInfo() {
11 return array(
12 'name' => 'Path alias functionality',
13 'description' => 'Add, edit, delete, and change alias and verify its consistency in the database.',
14 'group' => 'Path',
15 );
16 }
17
18 function setUp() {
19 parent::setUp('path');
20
21 // Create test user and login.
22 $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'administer url aliases', 'create url aliases'));
23 $this->drupalLogin($web_user);
24 }
25
26 /**
27 * Test the path cache.
28 */
29 function testPathCache() {
30 // Create test node.
31 $node1 = $this->drupalCreateNode();
32
33 // Create alias.
34 $edit = array();
35 $edit['source'] = 'node/' . $node1->nid;
36 $edit['alias'] = $this->randomName(8);
37 $this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
38
39 // Visit the system path for the node and confirm a cache entry is
40 // created.
41 cache_clear_all('*', 'cache_path', TRUE);
42 $this->drupalGet($edit['source']);
43 $this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
44
45 // Visit the alias for the node and confirm a cache entry is created.
46 cache_clear_all('*', 'cache_path', TRUE);
47 $this->drupalGet($edit['alias']);
48 $this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
49 }
50
51 /**
52 * Test alias functionality through the admin interfaces.
53 */
54 function testAdminAlias() {
55 // Create test node.
56 $node1 = $this->drupalCreateNode();
57
58 // Create alias.
59 $edit = array();
60 $edit['source'] = 'node/' . $node1->nid;
61 $edit['alias'] = $this->randomName(8);
62 $this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
63
64 // Confirm that the alias works.
65 $this->drupalGet($edit['alias']);
66 $this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Alias works.');
67
68 // Change alias.
69 $pid = $this->getPID($edit['alias']);
70
71 $previous = $edit['alias'];
72 $edit['alias'] = $this->randomName(8);
73 $this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Update alias'));
74
75 // Confirm that the alias works.
76 $this->drupalGet($edit['alias']);
77 $this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Changed alias works.');
78
79 drupal_static_reset('drupal_lookup_path');
80 // Confirm that previous alias no longer works.
81 $this->drupalGet($previous);
82 $this->assertNoText($node1->title, 'Previous alias no longer works.');
83 $this->assertResponse(404);
84
85 // Create second test node.
86 $node2 = $this->drupalCreateNode();
87
88 // Set alias to second test node.
89 $edit['source'] = 'node/' . $node2->nid;
90 // leave $edit['alias'] the same
91 $this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
92
93 // Confirm no duplicate was created.
94 $this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['alias'])), 'Attempt to move alias was rejected.');
95
96 // Delete alias.
97 $this->drupalPost('admin/config/search/path/delete/' . $pid, array(), t('Confirm'));
98
99 // Confirm that the alias no longer works.
100 $this->drupalGet($edit['alias']);
101 $this->assertNoText($node1->title, 'Alias was successfully deleted.');
102 }
103
104 /**
105 * Test alias functionality through the node interfaces.
106 */
107 function testNodeAlias() {
108 // Create test node.
109 $node1 = $this->drupalCreateNode();
110
111 // Create alias.
112 $edit = array();
113 $edit['path[alias]'] = $this->randomName(8);
114 $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
115
116 // Confirm that the alias works.
117 $this->drupalGet($edit['path[alias]']);
118 $this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Alias works.');
119
120 // Change alias.
121 $previous = $edit['path[alias]'];
122 $edit['path[alias]'] = $this->randomName(8);
123 $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
124
125 // Confirm that the alias works.
126 $this->drupalGet($edit['path[alias]']);
127 $this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Changed alias works.');
128
129 // Make sure that previous alias no longer works.
130 $this->drupalGet($previous);
131 $this->assertNoText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Previous alias no longer works.');
132 $this->assertResponse(404);
133
134 // Create second test node.
135 $node2 = $this->drupalCreateNode();
136
137 // Set alias to second test node.
138 // Leave $edit['path[alias]'] the same.
139 $this->drupalPost('node/' . $node2->nid . '/edit', $edit, t('Save'));
140
141 // Confirm that the alias didn't make a duplicate.
142 $this->assertText(t('The alias is already in use.'), 'Attempt to moved alias was rejected.');
143
144 // Delete alias.
145 $this->drupalPost('node/' . $node1->nid . '/edit', array('path[alias]' => ''), t('Save'));
146
147 // Confirm that the alias no longer works.
148 $this->drupalGet($edit['path[alias]']);
149 $this->assertNoText($node1->title, 'Alias was successfully deleted.');
150 }
151
152 function getPID($alias) {
153 return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField();
154 }
155 }
156
157 /**
158 * Test URL aliases for taxonomy terms.
159 */
160 class PathTaxonomyTermTestCase extends DrupalWebTestCase {
161 public static function getInfo() {
162 return array(
163 'name' => 'Taxonomy term URL aliases',
164 'description' => 'Tests URL aliases for taxonomy terms.',
165 'group' => 'Path',
166 );
167 }
168
169 function setUp() {
170 parent::setUp('path', 'taxonomy');
171
172 // Create and login user.
173 $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages'));
174 $this->drupalLogin($web_user);
175 }
176
177 /**
178 * Test alias functionality through the admin interfaces.
179 */
180 function testTermAlias() {
181 // Create a term in the default 'Tags' vocabulary with URL alias.
182 $edit = array();
183 $edit['name'] = $this->randomName();
184 $edit['description'] = $this->randomName();
185 $edit['path[alias]'] = $this->randomName();
186 $this->drupalPost('admin/structure/taxonomy/1/list/add', $edit, t('Save'));
187
188 // Confirm that the alias works.
189 $this->drupalGet($edit['path[alias]']);
190 $this->assertText($edit['description'], 'Term can be accessed on URL alias.');
191
192 // Change the term's URL alias.
193 $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
194 $edit2 = array();
195 $edit2['path[alias]'] = $this->randomName();
196 $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
197
198 // Confirm that the changed alias works.
199 $this->drupalGet($edit2['path[alias]']);
200 $this->assertText($edit['description'], 'Term can be accessed on changed URL alias.');
201
202 // Confirm that the old alias no longer works.
203 $this->drupalGet($edit['path[alias]']);
204 $this->assertNoText($edit['description'], 'Old URL alias has been removed after altering.');
205 $this->assertResponse(404, 'Old URL alias returns 404.');
206
207 // Remove the term's URL alias.
208 $edit3 = array();
209 $edit3['path[alias]'] = '';
210 $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
211
212 // Confirm that the alias no longer works.
213 $this->drupalGet($edit2['path[alias]']);
214 $this->assertNoText($edit['description'], 'Old URL alias has been removed after altering.');
215 $this->assertResponse(404, 'Old URL alias returns 404.');
216 }
217 }
218
219 class PathLanguageTestCase extends DrupalWebTestCase {
220 public static function getInfo() {
221 return array(
222 'name' => 'Path aliases with translated nodes',
223 'description' => 'Confirm that paths work with translated nodes',
224 'group' => 'Path',
225 );
226 }
227
228 function setUp() {
229 parent::setUp('path', 'locale', 'translation');
230
231 // Create and login user.
232 $web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'translate content', 'access administration pages'));
233 $this->drupalLogin($web_user);
234
235 // Enable French language.
236 $edit = array();
237 $edit['langcode'] = 'fr';
238
239 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
240
241 // Set language negotiation to "Path prefix with fallback".
242 include_once DRUPAL_ROOT . '/includes/locale.inc';
243 variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
244 variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
245
246 // Force inclusion of language.inc.
247 drupal_language_initialize();
248 }
249
250 /**
251 * Test alias functionality through the admin interfaces.
252 */
253 function testAliasTranslation() {
254 // Set 'page' content type to enable translation.
255 variable_set('language_content_type_page', 2);
256
257 $english_node = $this->drupalCreateNode(array('type' => 'page'));
258
259 // Edit the node to set language and path.
260 $edit = array();
261 $edit['language'] = 'en';
262 $edit['path[alias]'] = $this->randomName();
263 $this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save'));
264
265 // Confirm that the alias works.
266 $this->drupalGet($edit['path[alias]']);
267 $this->assertText($english_node->title[FIELD_LANGUAGE_NONE][0]['value'], 'Alias works.');
268
269 // Translate the node into French.
270 $this->drupalGet('node/' . $english_node->nid . '/translate');
271 $this->clickLink(t('add translation'));
272 $edit = array();
273 $langcode = 'fr';
274 $edit["body[$langcode][0][value]"] = $this->randomName();
275 $langcode = FIELD_LANGUAGE_NONE;
276 $edit["title[$langcode][0][value]"] = $this->randomName();
277 $edit['path[alias]'] = $this->randomName();
278 $this->drupalPost(NULL, $edit, t('Save'));
279
280 // Clear the path lookup cache.
281 drupal_lookup_path('wipe');
282
283 // Ensure the node was created.
284 $french_node = $this->drupalGetNodeByTitle($edit["title[$langcode][0][value]"]);
285 $this->assertTrue(($french_node), 'Node found in database.');
286
287 // Confirm that the alias works.
288 $this->drupalGet('fr/' . $edit['path[alias]']);
289 $this->assertText($french_node->title[FIELD_LANGUAGE_NONE][0]['value'], 'Alias for French translation works.');
290
291 // Confirm that the alias is returned by url().
292 drupal_static_reset('language_list');
293 $languages = language_list();
294 $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->language]));
295 $this->assertTrue(strpos($url, $edit['path[alias]']), t('URL contains the path alias.'));
296 }
297 }

  ViewVC Help
Powered by ViewVC 1.1.2