<?php

/**
 * @file
 * Tests for Metatag for when i18n is enabled and actively being used.
 */

/**
 * Tests for Metatag for when i18n is enabled and actively being used.
 */
class MetatagCoreWithI18nOutputTest extends MetatagTestHelper {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Metatag core tests with i18n: output',
      'description' => 'Test Metatag integration with the i18n module.',
      'group' => 'Metatag',
      'dependencies' => array('ctools', 'token', 'i18n'),
    );
  }

  /**
   * {@inheritdoc}
   */
  function setUp(array $modules = array()) {
    // Need the i18n and i18n_strings modules.
    $modules[] = 'i18n';
    $modules[] = 'i18n_string';

    // Enable all of the modules that are needed.
    parent::setUp($modules);

    // Add more locales.
    $this->setupLocales();

    // Set up the locales.
    $perms = array(
      'administer languages',
      'translate interface',
      // From i18n.
      'translate admin strings',
      'translate user-defined strings',
    );
    $this->adminUser = $this->createAdminUser($perms);

    // Log in the admin user.
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Test translations of the output tags.
   */
  public function testI18nOutputTranslationsEnabled() {
    // Enable output translations.
    $this->toggleOutputTranslations(TRUE);

    // Plan out the different translation string tests.
    $string_en = 'Welcome to the homepage!';
    $string_fr = 'Bonjour pour le homepage!';
    $string_context = 'output:frontpage:title';

    $this->searchTranslationPage('', $string_context);

    // Confirm the string is not present yet.
    $this->searchTranslationPage($string_en, $string_context, FALSE);

    // Load the front page's meta tags.
    $instance = 'global:frontpage';
    $config = metatag_config_load($instance);

    // Set something specific as the homepage.
    $config->config['title']['value'] = $string_en;
    metatag_config_save($config);

    // Load the front page.
    $this->drupalGet('');
    $this->assertResponse(200, 'Loaded the homepage.');

    // Confirm the page's title is what we set it to.
    $this->assertTitle($string_en);

    // Confirm the string is translatable.
    $this->searchTranslationPage($string_en, $string_context);

    // Get the translation string lid for the front page's title.
    $lid = $this->getTranslationLidByContext($string_context);
    $this->assertNotEqual($lid, 0, 'Found the locales_source string for the front page output title tag.');

    // Save the translation string.
    $this->saveTranslationString($lid, $string_context, 'fr', $string_en, $string_fr);

    // Load the English front page again.
    $this->drupalGet('');
    $this->assertResponse(200, 'Loaded the default homepage.');

    // Confirm the page's title is what we set it to.
    $this->assertTitle($string_en);

    // Load the French front page.
    $this->drupalGet('fr');
    $this->assertResponse(200, 'Loaded the French homepage.');

    // Confirm the page's title is what we set it to.
    $this->assertTitle($string_fr);

    // Make doubly sure there are output translations.
    $lids = $this->getTranslationsByContext($string_context);
    $this->assertNotEqual($lids, array());
  }

  /**
   * Verify that no output translation records are generated when it's disabled.
   */
  public function testI18nOutputTranslationsDisabled() {
    // Make sure output translations are disabled.
    $this->toggleOutputTranslations(FALSE);

    // Plan out the different translation string tests.
    $string_en = 'Welcome to the homepage!';
    $string_fr = 'Bonjour pour le homepage!';
    $string_context = 'output:frontpage:title';

    $this->searchTranslationPage('', $string_context);

    // Confirm the string is not present yet.
    $this->searchTranslationPage($string_en, $string_context, FALSE);

    // Load the front page's meta tags.
    $instance = 'global:frontpage';
    $config = metatag_config_load($instance);

    // Set something specific as the homepage.
    $config->config['title']['value'] = $string_en;
    metatag_config_save($config);

    // Load the front page.
    $this->drupalGet('');
    $this->assertResponse(200, 'Loaded the homepage.');

    // Confirm the page's title is what we set it to.
    $this->assertTitle($string_en);

    // Confirm the string is still not translatable.
    $this->searchTranslationPage($string_en, $string_context, FALSE);

    // Make doubly sure there are no output translations.
    $lids = $this->getTranslationsByContext($string_context);
    $this->assertEqual($lids, array());
  }

  /**
   *
   */
  public function testMenuPaths() {
    $paths = metatag_test_menu();
    $test_string = 'read-more-books-every-day';
    foreach ($paths as $path => $menu_item) {
      if (substr($path, -1) == '%') {
        $path = substr($path, 0, -1) . $test_string;
      }
      $this->drupalGet($path);
      $this->assertResponse(200, 'Loaded a page: ' . $path);
    }
  }

  /**
   * Enable or disable output translations.
   *
   * @param bool $enable
   *   Whether or not to enable translations; defaults to TRUE.
   */
  private function toggleOutputTranslations($enable = TRUE) {
    // Enable output translation.
    variable_set('metatag_i18n_translate_output', $enable);

    // Reload the translations.
    drupal_flush_all_caches();
    module_load_include('admin.inc', 'i18n_string');
    i18n_string_refresh_group('metatag');
  }

}