<?php
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() {
    
    $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',
    ));
  }
  
  public function testLocaleUpgrade() {
    $this
      ->assertTrue($this
      ->performUpgrade(), 'The upgrade was completed successfully.');
    
    $this
      ->assertPageInLanguage('', 'fr');
    
    $this
      ->drupalGet('en');
    $this
      ->assertResponse(404);
    $this
      ->drupalGet('fr');
    $this
      ->assertResponse(404);
  }
  
  public function testLocaleUpgradePathDefault() {
    
    $this
      ->variable_set('language_negotiation', 1);
    $this
      ->assertTrue($this
      ->performUpgrade(), 'The upgrade was completed successfully.');
    
    $this
      ->assertPageInLanguage('', 'fr');
    
    $this
      ->assertRaw('block-locale-language', 'The language switcher block is displayed.');
    
    $this
      ->drupalGet('fr');
    $this
      ->assertResponse(404);
    
    $this
      ->assertPageInLanguage('en', 'en');
  }
  
  public function testLocaleUpgradePathFallback() {
    
    $this
      ->variable_set('language_negotiation', 2);
    
    db_update('users')
      ->fields(array(
      'language' => 'en',
    ))
      ->condition('uid', 1)
      ->execute();
    $this
      ->assertTrue($this
      ->performUpgrade(), 'The upgrade was completed successfully.');
    
    $this
      ->assertPageInLanguage('fr', 'fr');
    $this
      ->assertPageInLanguage('en', 'en');
    
    $this
      ->assertPageInLanguage('', 'en');
    
    $this
      ->assertRaw('block-locale-language', 'The language switcher block is displayed.');
  }
  
  public function testLocaleUpgradeDomain() {
    
    $this
      ->variable_set('language_negotiation', 3);
    $this
      ->assertTrue($this
      ->performUpgrade(), 'The upgrade was completed successfully.');
    
    $this
      ->assertPageInLanguage('', 'fr');
    
    $this
      ->assertRaw('block-locale-language', 'The language switcher block is displayed.');
    
    $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, 'The English link points to the correct domain.');
    
    $this
      ->drupalGet('en');
    $this
      ->assertResponse(404);
    $this
      ->drupalGet('fr');
    $this
      ->assertResponse(404);
  }
  
  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;
    }
  }
}