Projet

Général

Profil

Paste
Télécharger (7,35 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.multilingual.test @ 8e7483ab

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests that exercise the relative / absolute output of the link module.
6
 */
7

    
8
/**
9
 * Testing that absolute / relative outputs match the expected format.
10
 */
11
class LinkMultilingualTestCase extends LinkBaseTestClass {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function setUp(array $modules = array()) {
17
    $this->permissions = array_merge($this->permissions, array(
18
      'administer site configuration',
19
      'administer languages',
20
    ));
21
    $modules[] = 'locale';
22
    $modules[] = 'locale_test';
23
    $modules[] = 'translation';
24
    parent::setUp($modules);
25
  }
26

    
27
  /**
28
   * Enables and configured language related stuff.
29
   */
30
  public function setUpLanguage() {
31
    global $language_url;
32
    $this->drupalGet('admin/config/regional/language');
33
    // Enable the path prefix for the default language: this way any un-prefixed
34
    // URL must have a valid fallback value.
35
    $edit = array('prefix' => 'en');
36
    $this->drupalPost('admin/config/regional/language/edit/en', $edit, t('Save language'));
37
    $language_url->prefix = $language_url->language;
38

    
39
    // Add custom language - as we need more than 1 language to be multilingual.
40
    // Code for the language.
41
    $langcode = 'xx';
42
    // The English name for the language.
43
    $name = $this->randomName(16);
44
    // The native name for the language.
45
    $native = $this->randomName(16);
46
    $edit = array(
47
      'langcode' => $langcode,
48
      'name' => $name,
49
      'native' => $native,
50
      'prefix' => $langcode,
51
      'direction' => '0',
52
    );
53
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
54
    variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
55

    
56
    // Enable URL language detection and selection.
57
    $edit = array('language[enabled][locale-url]' => 1);
58
    $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
59
    language_negotiation_set(LANGUAGE_TYPE_INTERFACE, array(LOCALE_LANGUAGE_NEGOTIATION_URL));
60

    
61
    // Reset static caching.
62
    drupal_static_reset('language_list');
63
    drupal_static_reset('locale_url_outbound_alter');
64
    drupal_static_reset('locale_language_url_rewrite_url');
65
  }
66

    
67
}
68
/**
69
 *
70
 */
71
class LinkMultilingualPathTest extends LinkMultilingualTestCase {
72

    
73
  /**
74
   *
75
   */
76
  public static function getInfo() {
77
    return array(
78
      'name' => 'Link language path prefix',
79
      'description' => 'Tests that path properly work with language path prefixes.',
80
      'group' => 'Link',
81
    );
82
  }
83

    
84
  /**
85
   * Creates a link field, fills it, then uses a loaded node to test paths.
86
   */
87
  public function testLanguagePrefixedPaths() {
88
    $this->setUpLanguage();
89

    
90
    // Create fields.
91
    // Field for absolute urls.
92
    $field_name_absolute = $this->createLinkField('page');
93

    
94
    // Field for relative urls.
95
    $settings = array(
96
      'instance[settings][absolute_url]' => FALSE,
97
    );
98
    $field_name_relative = $this->createLinkField('page', $settings);
99

    
100
    // Check the node edit form.
101
    $this->drupalGet('node/add/page');
102
    $this->assertField($field_name_absolute . '[und][0][title]', 'Title absolute found');
103
    $this->assertField($field_name_absolute . '[und][0][url]', 'URL absolute found');
104
    $this->assertField($field_name_relative . '[und][0][title]', 'Title relative found');
105
    $this->assertField($field_name_relative . '[und][0][url]', 'URL relative found');
106

    
107
    // Create test content.
108
    $url_tests = array(
109
      1 => array(
110
        'href' => 'http://dummy.com/' . $this->randomName(),
111
        'label' => $this->randomName(),
112
      ),
113
      2 => array(
114
        'href' => 'node/1',
115
        'label' => $this->randomName(),
116
      ),
117
      3 => array(
118
        'href' => 'node/1?property=value',
119
        'label' => $this->randomName(),
120
        'query' => array('property' => 'value'),
121
      ),
122
      4 => array(
123
        'href' => 'node/1#position',
124
        'label' => $this->randomName(),
125
        'fragment' => 'position',
126
      ),
127
      5 => array(
128
        'href' => 'node/1?property=value2#lower',
129
        'label' => $this->randomName(),
130
        'fragment' => 'lower',
131
        'query' => array('property' => 'value2'),
132
      ),
133
    );
134
    foreach ($url_tests as $index => &$input) {
135
      $this->drupalGet('node/add/page');
136

    
137
      $edit = array(
138
        'title' => $input['label'],
139
        $field_name_absolute . '[und][0][title]' => $input['label'],
140
        $field_name_absolute . '[und][0][url]' => $input['href'],
141
        $field_name_relative . '[und][0][title]' => $input['label'],
142
        $field_name_relative . '[und][0][url]' => $input['href'],
143
      );
144
      $this->drupalPost(NULL, $edit, t('Save'));
145
      $url = $this->getUrl();
146
      $input['url'] = $url;
147
    }
148

    
149
    // Change to anonymous user.
150
    $this->drupalLogout();
151

    
152
    foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
153
      $node = node_load($index);
154
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
155
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
156
      $this->drupalGet('node/' . $index);
157

    
158
      $relative_expected = url('node/1', array('absolute' => FALSE) + $input2);
159
      $absolute_expected = url('node/1', array('absolute' => TRUE) + $input2);
160

    
161
      $absolute_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
162
      $absolute_result = (string) reset($absolute_result);
163
      $this->assertEqual($absolute_result, $absolute_expected, "Absolute url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
164

    
165
      $relative_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
166
      $relative_result = (string) reset($relative_result);
167
      $this->assertEqual($relative_result, $relative_expected, "Relative url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
168
    }
169

    
170
    // Check if this works with the alias too.
171
    // Add a path alias for node 1.
172
    $path = array(
173
      'source' => 'node/1',
174
      'alias' => $url_tests[1]['label'],
175
    );
176
    path_save($path);
177
    // Another iteration over the same nodes - this time they should use the
178
    // path alias.
179
    foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
180
      $node = node_load($index);
181
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
182
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
183
      $this->drupalGet('node/' . $index);
184

    
185
      $relative_expected = url('node/1', array('absolute' => FALSE) + $input2);
186
      $absolute_expected = url('node/1', array('absolute' => TRUE) + $input2);
187

    
188
      $absolute_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
189
      $absolute_result = (string) reset($absolute_result);
190
      $this->assertEqual($absolute_result, $absolute_expected, "Absolute alias-url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
191

    
192
      $relative_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
193
      $relative_result = (string) reset($relative_result);
194
      $this->assertEqual($relative_result, $relative_expected, "Relative alias-url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
195
    }
196
  }
197

    
198
}