You are here

public function DbtngExampleTest::testUi in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/dbtng_example/tests/src/Functional/DbtngExampleTest.php \Drupal\Tests\dbtng_example\Functional\DbtngExampleTest::testUi()

Test the UI.

File

dbtng_example/tests/src/Functional/DbtngExampleTest.php, line 92

Class

DbtngExampleTest
Tests for the dbtng_example module.

Namespace

Drupal\Tests\dbtng_example\Functional

Code

public function testUi() {
  $assert = $this
    ->assertSession();
  $this
    ->drupalLogin($this
    ->createUser());

  // Test the basic list.
  $this
    ->drupalGet('/examples/dbtng-example');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextMatches('%John[td/<>\\w\\s]+Doe%');

  // Test the add tab.
  // Add the new entry.
  $this
    ->drupalPostForm('/examples/dbtng-example/add', [
    'name' => 'Some',
    'surname' => 'Anonymous',
    'age' => 33,
  ], 'Add');

  // Now find the new entry.
  $this
    ->drupalGet('/examples/dbtng-example');
  $assert
    ->pageTextMatches('%Some[td/<>\\w\\s]+Anonymous%');

  // Try the update tab.
  // Find out the pid of our "anonymous" guy.
  $result = $this->container
    ->get('dbtng_example.repository')
    ->load([
    'surname' => 'Anonymous',
  ]);
  $this
    ->drupalGet('/examples/dbtng-example');
  $this
    ->assertCount(1, $result, 'Did not find one entry in the table with surname = "Anonymous".');
  $entry = $result[0];
  unset($entry->uid);
  $entry = [
    'name' => 'NewFirstName',
    'age' => 22,
  ];
  $this
    ->drupalPostForm('/examples/dbtng-example/update', $entry, 'Update');

  // Now find the new entry.
  $this
    ->drupalGet('/examples/dbtng-example');
  $assert
    ->pageTextMatches('%NewFirstName[td/<>\\w\\s]+Anonymous%');

  // Try the advanced tab.
  $this
    ->drupalGet('/examples/dbtng-example/advanced');
  $rows = $this
    ->xpath("//*[@id='dbtng-example-advanced-list'][1]/tbody/tr");
  $this
    ->assertCount(1, $rows);
  $field = $this
    ->xpath("//*[@id='dbtng-example-advanced-list'][1]/tbody/tr/td[4]");
  $this
    ->assertEquals('Roe', $field[0]
    ->getText());

  // Try to add an entry while logged out.
  $this
    ->drupalLogout();
  $this
    ->drupalPostForm('/examples/dbtng-example/add', [
    'name' => 'Anonymous',
    'surname' => 'UserCannotPost',
    'age' => 'not a number',
  ], 'Add');
  $assert
    ->pageTextContains('You must be logged in to add values to the database.');
  $assert
    ->pageTextContains('Age needs to be a number');
}