summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/upgrade/upgrade.locale.test
blob: aec559de66f3003fcafb46d5fe2bff88074e7f61 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

/**
 * Upgrade test for locale.module.
 */
class LocaleUpgradePathTestCase extends UpgradePathTestCase {
  public static function getInfo() {
    return array(
      'name'  => 'Locale upgrade path',
      'description'  => 'Upgrade path tests for the Locale module.',
      'group' => 'Upgrade path',
    );
  }

  public function setUp() {
    // Path to the database dump files.
    $this->databaseDumpFiles = array(
      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.locale.database.php',
    );
    parent::setUp();

    $this->uninstallModulesExcept(array('locale', 'comment'));
  }

  /**
   * Test a successful upgrade (no negotiation).
   */
  public function testLocaleUpgrade() {
    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));

    // The home page should be in French.
    $this->assertPageInLanguage('', 'fr');

    // No prefixed page should exist.
    $this->drupalGet('en');
    $this->assertResponse(404);
    $this->drupalGet('fr');
    $this->assertResponse(404);
  }

  /**
   * Test an upgrade with path-based negotiation.
   */
  public function testLocaleUpgradePathDefault() {
    // LANGUAGE_NEGOTIATION_PATH_DEFAULT.
    $this->variable_set('language_negotiation', 1);

    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));

    // The home page should be in French.
    $this->assertPageInLanguage('', 'fr');

    // The language switcher block should be displayed.
    $this->assertRaw('block-locale-language', t('The language switcher block is displayed.'));

    // The French prefix should not be active because French is the default language.
    $this->drupalGet('fr');
    $this->assertResponse(404);

    // The English prefix should be active.
    $this->assertPageInLanguage('en', 'en');
  }

  /**
   * Test an upgrade with path-based (with fallback) negotiation.
   */
  public function testLocaleUpgradePathFallback() {
    // LANGUAGE_NEGOTIATION_PATH.
    $this->variable_set('language_negotiation', 2);

    // Set the language of the admin user to English.
    db_update('users')
      ->fields(array('language' => 'en'))
      ->condition('uid', 1)
      ->execute();

    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));

    // Both prefixes should be active.
    $this->assertPageInLanguage('fr', 'fr');
    $this->assertPageInLanguage('en', 'en');

    // The home page should be in the admin user language.
    $this->assertPageInLanguage('', 'en');

    // The language switcher block should be displayed.
    $this->assertRaw('block-locale-language', t('The language switcher block is displayed.'));
  }

  /**
   * Test an upgrade with domain-based negotiation.
   */
  public function testLocaleUpgradeDomain() {
    // LANGUAGE_NEGOTIATION_DOMAIN.
    $this->variable_set('language_negotiation', 3);

    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));

    // The home page should be in French.
    $this->assertPageInLanguage('', 'fr');

    // The language switcher block should be displayed.
    $this->assertRaw('block-locale-language', t('The language switcher block is displayed.'));

    // The language switcher block should point to http://en.example.com.
    $language_links = $this->xpath('//ul[contains(@class, :class)]/li/a', array(':class' => 'language-switcher-locale-url'));
    $found_english_link = FALSE;
    foreach ($language_links as $link) {
      if ((string) $link['href'] == 'http://en.example.com/') {
        $found_english_link = TRUE;
      }
    }
    $this->assertTrue($found_english_link, t('The English link points to the correct domain.'));

    // Both prefixes should be inactive.
    $this->drupalGet('en');
    $this->assertResponse(404);
    $this->drupalGet('fr');
    $this->assertResponse(404);

  }

  /**
   * Asserts that a page exists and is in the specified language.
   */
  public function assertPageInLanguage($path = NULL, $langcode) {
    if (isset($path)) {
      $this->drupalGet($path);
    }

    if (!$this->assertResponse(200)) {
      return FALSE;
    }

    if ($this->parse()) {
      return $this->assertIdentical($langcode, (string) $this->elements['xml:lang']);
    }
    else {
      return FALSE;
    }
  }
}