outline-8.x-1.x-dev/tests/src/Functional/EntryLanguageTest.php

tests/src/Functional/EntryLanguageTest.php
<?php

namespace Drupal\Tests\outline\Functional;

use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;

/**
 * Tests the language functionality for the outline entries.
 *
 * @group outline
 */
class EntryLanguageTest extends OutlineTestBase {

  protected static $modules = ['language'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Outline for testing.
   *
   * @var \Drupal\outline\OutlineInterface
   */
  protected $outline;

  protected function setUp(): void {
    parent::setUp();

    // Create an administrative user.
    $this->drupalLogin($this->drupalCreateUser(['administer outline']));

    // Create a outline to which the entries will be assigned.
    $this->outline = $this->createOutline();

    // Add some custom languages.
    foreach (['aa', 'bb', 'cc'] as $language_code) {
      ConfigurableLanguage::create([
        'id' => $language_code,
        'label' => $this->randomMachineName(),
      ])->save();
    }
  }

  public function testEntryLanguage() {
    // Configure the outline to not hide the language selector.
    $edit = [
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/outline/manage/' . $this->outline->id(), $edit, t('Save'));

    // Add a entry.
    $this->drupalGet('admin/structure/outline/manage/' . $this->outline->id() . '/add');
    // Check that we have the language selector.
    $this->assertField('edit-langcode-0-value', t('The language selector field was found on the page.'));
    // Submit the entry.
    $edit = [
      'name[0][value]' => $this->randomMachineName(),
      'langcode[0][value]' => 'aa',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $entries = outline_entry_load_multiple_by_name($edit['name[0][value]']);
    $entry = reset($entries);
    $this->assertEqual($entry->language()->getId(), $edit['langcode[0][value]'], 'The entry contains the correct langcode.');

    // Check if on the edit page the language is correct.
    $this->drupalGet('outline/entry/' . $entry->id() . '/edit');
    $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The entry language was correctly selected.');

    // Change the language of the entry.
    $edit['langcode[0][value]'] = 'bb';
    $this->drupalPostForm('outline/entry/' . $entry->id() . '/edit', $edit, t('Save'));

    // Check again that on the edit page the language is correct.
    $this->drupalGet('outline/entry/' . $entry->id() . '/edit');
    $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The entry language was correctly selected.');
  }

  public function testDefaultEntryLanguage() {
    // Configure the outline to not hide the language selector, and make the
    // default language of the entries fixed.
    $edit = [
      'default_language[langcode]' => 'bb',
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/outline/manage/' . $this->outline->id(), $edit, t('Save'));
    $this->drupalGet('admin/structure/outline/manage/' . $this->outline->id() . '/add');
    $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.');

    // Make the default language of the entries to be the current interface.
    $edit = [
      'default_language[langcode]' => 'current_interface',
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/outline/manage/' . $this->outline->id(), $edit, t('Save'));
    $this->drupalGet('aa/admin/structure/outline/manage/' . $this->outline->id() . '/add');
    $this->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected.");
    $this->drupalGet('bb/admin/structure/outline/manage/' . $this->outline->id() . '/add');
    $this->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected.");

    // Change the default language of the site and check if the default entries
    // language is still correctly selected.
    $this->config('system.site')->set('default_langcode', 'cc')->save();
    $edit = [
      'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/outline/manage/' . $this->outline->id(), $edit, t('Save'));
    $this->drupalGet('admin/structure/outline/manage/' . $this->outline->id() . '/add');
    $this->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected.");
  }

  /**
   * Tests that translated entries are displayed correctly on the entry overview.
   */
  public function testEntryTranslatedOnOverviewPage() {
    // Configure the outline to not hide the language selector.
    $edit = [
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/outline/manage/' . $this->outline->id(), $edit, t('Save'));

    // Add a entry.
    $this->drupalGet('admin/structure/outline/manage/' . $this->outline->id() . '/add');
    // Submit the entry.
    $edit = [
      'name[0][value]' => $this->randomMachineName(),
      'langcode[0][value]' => 'aa',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $entries = outline_entry_load_multiple_by_name($edit['name[0][value]']);
    $entry = reset($entries);

    // Add a translation for that entry.
    $translated_title = $this->randomMachineName();
    $entry->addTranslation('bb', [
      'name' => $translated_title,
    ]);
    $entry->save();

    // Overview page in the other language shows the translated entry
    $this->drupalGet('bb/admin/structure/outline/manage/' . $this->outline->id() . '/overview');
    $this->assertPattern('|<a[^>]*>' . $translated_title . '</a>|');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc