content_language_access.test in Content Language Access 6
Same filename and directory in other branches
Test suite for content language access module
File
content_language_access.testView source
<?php
/**
* @file
* Test suite for content language access module
*/
class ContentLanguageAccessTestCase extends DrupalWebTestCase {
/**
* Implements getInfo().
*/
public 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.
variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
}
/**
* 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['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/settings/language/add', $edit, t('Add language'));
// Make sure not using cached version.
$languages = language_list('language', TRUE);
$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) {
foreach ($languages as $language) {
if ($language->prefix) {
$prefix = $language->prefix . '/';
}
else {
$prefix = '';
}
$this
->drupalGet($prefix . 'node/' . $node->nid);
if ($node->language == '' || $node->language == $language->language) {
$this
->assertResponse(200);
}
else {
$this
->assertResponse(403);
}
}
}
}
}
Classes
Name | Description |
---|---|
ContentLanguageAccessTestCase | @file Test suite for content language access module |