class ConfigExampleTest

Ensure that the form_api_example forms work properly.

@group examples @group config_simple_example

Hierarchy

Expanded class hierarchy of ConfigExampleTest

Related topics

File

modules/config_simple_example/tests/src/Functional/ConfigExampleTest.php, line 17

Namespace

Drupal\Tests\form_api_example\Functional
View source
class ConfigExampleTest extends ExamplesBrowserTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Admin user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $adminUser;
  
  /**
   * Our module dependencies.
   *
   * @var string[]
   */
  protected static $modules = [
    'language',
    'locale',
    'node',
    'config_translation',
    'config_simple_example',
  ];
  
  /**
   * The installation profile to use with this test.
   *
   * @var string
   */
  protected $profile = 'minimal';
  
  /**
   * Permissions to operate the config simple form.
   *
   * @var array
   */
  protected $permissions = [
    'administer site configuration',
    'access administration pages',
    'access content',
    'translate configuration',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    // Login is required to access the config simple form.
    $this->adminUser = $this->drupalCreateUser($this->permissions);
    $this->drupalLogin($this->adminUser);
    // Enable the Spanish language.
    ConfigurableLanguage::createFromLangcode('es')->save();
  }
  
  /**
   * Aggregate all the tests.
   *
   * Since this is a functional test, and we don't expect to need isolation
   * between these form tests, we'll aggregate them here for speed's sake. That
   * way the testing system doesn't have to rebuild a new Drupal for us for each
   * test.
   */
  public function testFunctional() {
    // Please fail this one first.
    $this->doTestMainPage();
    $this->doTestConfigSimpleForm();
  }
  
  /**
   * Test the main page.
   */
  public function doTestMainPage() {
    $assert = $this->assertSession();
    $example_page = Url::fromRoute('config_simple_example.description');
    $this->drupalGet($example_page);
    $assert->pageTextContains('The Config Simple Example module defines a simple translatable config form.');
    $assert->linkExists('Config Simple Form');
  }
  
  /**
   * Test the config simple form.
   */
  public function doTestConfigSimpleForm() {
    $config_page_en = Url::fromRoute('config_simple_example.settings');
    $config_page_translate = $this->drupalGet('admin/config/form-api-example/config-simple-form/translate');
    $config_page_es = $this->drupalGet('admin/config/form-api-example/config-simple-form/translate/es/edit');
    // English.
    $assert = $this->assertSession();
    $this->drupalGet($config_page_en);
    $assert->fieldEnabled('edit-message');
    $assert->buttonExists('edit-submit');
    // Ensure the 'Save configuration' action performs the save.
    $this->drupalGet($config_page_en);
    $edit = [
      'message' => 'Message in EN',
    ];
    $this->submitForm($edit, 'Save configuration');
    $assert->pageTextContains('Message in EN');
    // Form translation page and click the button to Add Spanish translation.
    $this->drupalGet($config_page_translate);
    $this->clickLink('Add');
    $assert->fieldExists('translation[config_names][config_simple_example.settings][message]');
    // Ensure the 'Save translation' action performs the save.
    $edit = [
      'message' => 'Message in ES',
    ];
    $this->submitForm($edit, 'Save translation');
    $this->drupalGet($config_page_es);
    $assert->pageTextContains('Message in ES');
    // Go back to original English version and make sure it's still there.
    $this->drupalGet($config_page_en);
    $assert->pageTextContains('Message in EN');
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConfigExampleTest::$adminUser protected property Admin user.
ConfigExampleTest::$defaultTheme protected property
ConfigExampleTest::$modules protected static property Our module dependencies. Overrides ExamplesBrowserTestBase::$modules
ConfigExampleTest::$permissions protected property Permissions to operate the config simple form.
ConfigExampleTest::$profile protected property The installation profile to use with this test.
ConfigExampleTest::doTestConfigSimpleForm public function Test the config simple form.
ConfigExampleTest::doTestMainPage public function Test the main page.
ConfigExampleTest::setUp protected function Overrides ExamplesBrowserTestBase::setUp
ConfigExampleTest::testFunctional public function Aggregate all the tests.
ExamplesBrowserTestBase::setupExamplesMenus protected function Set up menus and tasks in their regions.