You are here

content_language_access.test in Content Language Access 7

Same filename and directory in other branches
  1. 6 content_language_access.test

Test suite for content language access module.

File

content_language_access.test
View source
<?php

/**
 * @file
 * Test suite for content language access module.
 */

/**
 * Defines the content language access test class.
 */
class ContentLanguageAccessTestCase extends DrupalWebTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Content Language Access'),
      'description' => t('Test the features of content_language_access module.'),
      'group' => t('Content Language Access'),
    );
  }

  /**
   * Implements setUp().
   */
  public function setUp() {
    parent::setUp('node', 'locale', 'content_language_access');
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer languages',
      'administer site configuration',
      'access administration pages',
      'administer content types',
      'administer nodes',
      'administer users',
    ));
    $this->visitor = $this
      ->drupalCreateUser(array(
      'access content',
    ));
    $this
      ->configureLanguages();
    $this
      ->createContentType();
    $this
      ->createContents();
  }

  /**
   * Creates the languages for the test execution.
   */
  protected function configureLanguages() {
    $this
      ->drupalLogin($this->admin_user);
    $this
      ->addLanguage('fr');
    $this
      ->addLanguage('pt-br');

    // Set path languages so we can retrieve pages in different languages.
    $negotiation = array(
      LOCALE_LANGUAGE_NEGOTIATION_URL => 1,
    );
    language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $negotiation);
  }

  /**
   * Creates a randon content type for test execution.
   */
  protected function createContentType() {
    $settings = array();

    // Set the content type multilangual suport to full.
    $settings['language_content_type'] = 2;
    $this->content_type = $this
      ->drupalCreateContentType($settings);
  }

  /**
   * Creates a content for each language for the tests.
   */
  protected function createContents() {
    $this
      ->drupalLogin($this->admin_user);
    $languages = $this
      ->getLanguageList();
    foreach ($languages as $language_key => $language) {
      $settings = array();
      $settings['title'] = 'Test ' . $language->name;
      $settings['body'][$language_key][0] = array();
      $settings['language'] = $language_key;
      $this->nodes[$language_key] = $this
        ->drupalCreateNode($settings);
    }
  }

  /**
   * Returns the list of languages available.
   *
   * @param bool $with_neutral_language
   *   Optional, specifies if the function needs to return also the neutral
   *   language.
   *
   * @return array
   *   With all the languagues available (plus the neutral language)
   */
  protected function getLanguageList($with_neutral_language = TRUE) {
    $languages = language_list();
    if ($with_neutral_language) {
      $languages[''] = (object) array(
        'prefix' => '',
        'language' => '',
        'name' => 'Language Neutral',
      );
    }
    return $languages;
  }

  /**
   * Enables the specified language if it has not been already.
   *
   * @param string $language_code
   *   The language code to enable.
   */
  protected function addLanguage($language_code) {

    // Check to make sure that language has not already been installed.
    $this
      ->drupalGet('admin/settings/language');
    if (strpos($this
      ->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {

      // Doesn't have language installed so add it.
      $edit = array();
      $edit['langcode'] = $language_code;
      $this
        ->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

      // Make sure not using cached version.
      drupal_static_reset('language_list');
      $languages = language_list('language');
      $this
        ->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
      if (array_key_exists($language_code, $languages)) {
        $this
          ->assertRaw(t('The language %language has been created and can now be used.', array(
          '%language' => $languages[$language_code]->name,
        )), t('Language has been created.'));
      }
    }
    else {

      // Ensure that it is enabled.
      $this
        ->drupalPost(NULL, array(
        'enabled[' . $language_code . ']' => TRUE,
      ), t('Save configuration'));
      $this
        ->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
    }
  }

  /**
   * Tests each content in each language.
   */
  public function testContentLanguageAccess() {
    $this
      ->drupalLogin($this->visitor);
    $languages = $this
      ->getLanguageList(FALSE);
    foreach ($this->nodes as $node) {
      $translation_enabled = locale_multilingual_node_type($node->type);
      foreach ($languages as $language) {
        if ($language->prefix) {
          $prefix = $language->prefix . '/';
        }
        else {
          $prefix = '';
        }
        $this
          ->drupalGet($prefix . 'node/' . $node->nid);
        if (!$translation_enabled || !$node->language || $node->language == LANGUAGE_NONE || $node->language == $language->language) {
          $this
            ->assertResponse(200);
        }
        else {
          $this
            ->assertResponse(403);
        }
      }
    }
  }

}

Classes

Namesort descending Description
ContentLanguageAccessTestCase Defines the content language access test class.