outline-8.x-1.x-dev/tests/src/Functional/OutlineLanguageTest.php
tests/src/Functional/OutlineLanguageTest.php
<?php
namespace Drupal\Tests\outline\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
/**
* Tests the language functionality for outlines.
*
* @group outline
*/
class OutlineLanguageTest extends OutlineTestBase {
protected static $modules = ['language'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
protected function setUp(): void {
parent::setUp();
// Create an administrative user.
$this->drupalLogin($this->drupalCreateUser(['administer outline']));
// Add some custom languages.
ConfigurableLanguage::create([
'id' => 'aa',
'label' => $this->randomMachineName(),
])->save();
ConfigurableLanguage::create([
'id' => 'bb',
'label' => $this->randomMachineName(),
])->save();
}
/**
* Tests language settings for outlines.
*/
public function testOutlineLanguage() {
$this->drupalGet('admin/structure/outline/add');
// Check that we have the language selector available.
$this->assertField('edit-langcode', 'The language selector field was found on the page.');
// Create the outline.
$oid = mb_strtolower($this->randomMachineName());
$edit['name'] = $this->randomMachineName();
$edit['description'] = $this->randomMachineName();
$edit['langcode'] = 'aa';
$edit['oid'] = $oid;
$this->drupalPostForm(NULL, $edit, t('Save'));
// Check the language on the edit page.
$this->drupalGet('admin/structure/outline/manage/' . $oid);
$this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The outline language was correctly selected.');
// Change the language and save again.
$edit['langcode'] = 'bb';
unset($edit['oid']);
$this->drupalPostForm(NULL, $edit, t('Save'));
// Check again the language on the edit page.
$this->drupalGet('admin/structure/outline/manage/' . $oid);
$this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The outline language was correctly selected.');
}
/**
* Tests entry language settings for outline entries are saved and updated.
*/
public function testOutlineDefaultLanguageForEntrys() {
// Add a new outline and check that the default language settings are for
// the entries are saved.
$edit = [
'name' => $this->randomMachineName(),
'oid' => mb_strtolower($this->randomMachineName()),
'default_language[langcode]' => 'bb',
'default_language[language_alterable]' => TRUE,
];
$oid = $edit['oid'];
$this->drupalPostForm('admin/structure/outline/add', $edit, t('Save'));
// Check that the outline was actually created.
$this->drupalGet('admin/structure/outline/manage/' . $edit['oid']);
$this->assertSession()->statusCodeEquals(200);
// Check that the language settings were saved.
$language_settings = ContentLanguageSettings::loadByEntityTypeBundle('outline_entry', $edit['oid']);
$this->assertEqual($language_settings->getDefaultLangcode(), 'bb', 'The langcode was saved.');
$this->assertTrue($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
// Check that the correct options are selected in the interface.
$this->assertOptionSelected('edit-default-language-langcode', 'bb', 'The correct default language for the entries of this outline is selected.');
$this->assertFieldChecked('edit-default-language-language-alterable', 'Show language selection option is checked.');
// Edit the outline and check that the new settings are updated.
$edit = [
'default_language[langcode]' => 'aa',
'default_language[language_alterable]' => FALSE,
];
$this->drupalPostForm('admin/structure/outline/manage/' . $oid, $edit, t('Save'));
// And check again the settings and also the interface.
$language_settings = ContentLanguageSettings::loadByEntityTypeBundle('outline_entry', $oid);
$this->assertEqual($language_settings->getDefaultLangcode(), 'aa', 'The langcode was saved.');
$this->assertFalse($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
$this->drupalGet('admin/structure/outline/manage/' . $oid);
$this->assertOptionSelected('edit-default-language-langcode', 'aa', 'The correct default language for the entries of this outline is selected.');
$this->assertNoFieldChecked('edit-default-language-language-alterable', 'Show language selection option is not checked.');
// Check that language settings are changed after editing outline.
$edit = [
'name' => $this->randomMachineName(),
'default_language[langcode]' => 'authors_default',
'default_language[language_alterable]' => FALSE,
];
$this->drupalPostForm('admin/structure/outline/manage/' . $oid, $edit, t('Save'));
// Check that we have the new settings.
$new_settings = ContentLanguageSettings::loadByEntityTypeBundle('outline_entry', $oid);
$this->assertEqual($new_settings->getDefaultLangcode(), 'authors_default', 'The langcode was saved.');
$this->assertFalse($new_settings->isLanguageAlterable(), 'The new visibility setting was saved.');
}
}
