path.test 7.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
// $Id$

class PathTestCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Path alias functionality'),
      'description' => t('Add, edit, delete, and change alias and verify its consistency in the database.'),
      'group' => t('Path'),
    );
  }

  /**
   * Create user, setup permissions, log user in, and create a node.
   */
  function setUp() {
    parent::setUp('path');
    // create and login user
    $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content', 'administer url aliases', 'create url aliases'));
    $this->drupalLogin($web_user);
  }

  /**
   * Test alias functionality through the admin interfaces.
   */
  function testAdminAlias() {
    // create test node
    $node1 = $this->createNode();

30
    // Create alias.
31 32 33 34 35
    $edit = array();
    $edit['src'] = 'node/' . $node1->nid;
    $edit['dst'] = $this->randomName(8);
    $this->drupalPost('admin/build/path/add', $edit, t('Create new alias'));

36
    // Confirm that the alias works.
37 38 39
    $this->drupalGet($edit['dst']);
    $this->assertText($node1->title, 'Alias works.');

40
    // Change alias.
41 42 43 44 45 46
    $pid = $this->getPID($edit['dst']);

    $previous = $edit['dst'];
    $edit['dst'] = $this->randomName(8);
    $this->drupalPost('admin/build/path/edit/' . $pid, $edit, t('Update alias'));

47
    // Confirm that the alias works.
48 49 50
    $this->drupalGet($edit['dst']);
    $this->assertText($node1->title, 'Changed alias works.');

51
    // Confirm that previous alias no longer works.
52 53 54 55
    $this->drupalGet($previous);
    $this->assertNoText($node1->title, 'Previous alias no longer works.');
    $this->assertResponse(404);

56
    // Create second test node.
57 58
    $node2 = $this->createNode();

59
    // Set alias to second test node.
60 61 62 63
    $edit['src'] = 'node/' . $node2->nid;
    // leave $edit['dst'] the same
    $this->drupalPost('admin/build/path/add', $edit, t('Create new alias'));

64
    // Confirm no duplicate was created.
65 66
    $this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['dst'])), 'Attempt to move alias was rejected.');

67
    // Delete alias.
68 69
    $this->drupalPost('admin/build/path/delete/' . $pid, array(), t('Confirm'));

70
    // Confirm that the alias no longer works.
71 72 73 74 75 76 77 78
    $this->drupalGet($edit['dst']);
    $this->assertNoText($node1->title, 'Alias was successfully deleted.');
  }

  /**
   * Test alias functionality through the node interfaces.
   */
  function testNodeAlias() {
79
    // Create test node.
80 81
    $node1 = $this->createNode();

82
    // Create alias.
83 84 85 86
    $edit = array();
    $edit['path'] = $this->randomName(8);
    $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));

87
    // Confirm that the alias works.
88 89 90
    $this->drupalGet($edit['path']);
    $this->assertText($node1->title, 'Alias works.');

91
    // Change alias.
92 93 94 95
    $previous = $edit['path'];
    $edit['path'] = $this->randomName(8);
    $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));

96
    // Confirm that the alias works.
97 98 99
    $this->drupalGet($edit['path']);
    $this->assertText($node1->title, 'Changed alias works.');

100
    // Make sure that previous alias no longer works.
101 102 103 104
    $this->drupalGet($previous);
    $this->assertNoText($node1->title, 'Previous alias no longer works.');
    $this->assertResponse(404);

105
    // Create second test node.
106 107
    $node2 = $this->createNode();

108 109
    // Set alias to second test node.
    // Leave $edit['path'] the same.
110 111
    $this->drupalPost('node/' . $node2->nid . '/edit', $edit, t('Save'));

112
    // Confirm that the alias didn't make a duplicate.
113 114
    $this->assertText(t('The path is already in use.'), 'Attempt to moved alias was rejected.');

115
    // Delete alias.
116 117
    $this->drupalPost('node/' . $node1->nid . '/edit', array('path' => ''), t('Save'));

118
    // Confirm that the alias no longer works.
119 120 121 122 123 124 125 126 127 128 129 130 131 132
    $this->drupalGet($edit['path']);
    $this->assertNoText($node1->title, 'Alias was successfully deleted.');
  }

  function getPID($dst) {
    return db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $dst));
  }

  function createNode() {
    $edit = array();
    $edit['title'] = '!SimpleTest test node! ' . $this->randomName(10);
    $edit['body'] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
    $this->drupalPost('node/add/page', $edit, t('Save'));

133
    // Check to make sure the node was created.
134
    $node = $this->drupalGetNodeByTitle($edit['title']);
135

136 137 138 139 140
    $this->assertNotNull(($node === FALSE ? NULL : $node), 'Node found in database. %s');

    return $node;
  }
}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

class PathLanguageTestCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Path aliases with translated nodes'),
      'description' => t('Confirm that paths work with translated nodes'),
      'group' => t('Path'),
    );
  }

  /**
   * Create user, setup permissions, log user in, and create a node.
   */
  function setUp() {
    parent::setUp('path', 'locale', 'translation');

    // Create and login user.
    $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'translate content', 'access administration pages'));
    $this->drupalLogin($web_user);

    // Enable French language.
    $edit = array();
    $edit['langcode'] = 'fr';

    $this->drupalPost('admin/settings/language/add', $edit, t('Add language'));

    // Set language negotiation to "Path prefix with fallback".
    variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);

    // Force inclusion of language.inc.
    drupal_init_language();
  }

  /**
   * Test alias functionality through the admin interfaces.
   */
  function testAliasTranslation() {
    // Set 'page' content type to enable translation.
    variable_set('language_content_type_page', 2);

    // Create a page in English.
    $edit = array();
    $edit['title'] = $this->randomName();
    $edit['body'] = $this->randomName();
    $edit['language'] = 'en';
    $edit['path'] = $this->randomName();

    $this->drupalPost('node/add/page', $edit, t('Save'));

    // Check to make sure the node was created.
191
    $english_node = $this->drupalGetNodeByTitle($edit['title']);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    $this->assertTrue(($english_node), 'Node found in database.');

    // Confirm that the alias works.
    $this->drupalGet($edit['path']);
    $this->assertText($english_node->title, 'Alias works.');

    // Translate the node into French.
    $this->drupalGet('node/' . $english_node->nid . '/translate');
    $this->clickLink(t('add translation'));
    $edit = array();
    $edit['title'] = $this->randomName();
    $edit['body'] = $this->randomName();
    $edit['path'] = $this->randomName();
    $this->drupalPost(NULL, $edit, t('Save'));

    // Clear the path lookup cache.
    drupal_lookup_path('wipe');

    // Ensure the node was created.
    // Check to make sure the node was created.
212
    $french_node = $this->drupalGetNodeByTitle($edit['title']);
213 214 215 216 217 218 219 220 221 222 223 224 225
    $this->assertTrue(($french_node), 'Node found in database.');

    // Confirm that the alias works.
    $this->drupalGet('fr/' . $edit['path']);
    $this->assertText($french_node->title, 'Alias for French translation works.');

    // Confirm that the alias is returned by url().
    $languages = language_list();
    $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->language]));
    $this->assertTrue(strpos($url, $edit['path']), t('URL contains the path alias.'));
  }
}