View source
<?php
namespace Drupal\Tests\dbtng_example\Functional;
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
class DbtngExampleTest extends ExamplesBrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'dbtng_example',
];
protected $profile = 'minimal';
public function testDbtngExample() {
$assert = $this
->assertSession();
$result = $this->container
->get('dbtng_example.repository')
->load();
$this
->assertCount(2, $result, 'Did not find two entries in the table after installing the module.');
$this
->drupalGet('/examples/dbtng-example');
$assert
->statusCodeEquals(200);
$links = $this
->providerMenuLinks();
foreach ($links as $page => $hrefs) {
foreach ($hrefs as $href) {
$this
->drupalGet($page);
$assert
->linkByHrefExists($href);
}
}
}
protected function providerMenuLinks() {
return [
'' => [
'/examples/dbtng-example',
],
'/examples/dbtng-example' => [
'/examples/dbtng-example/add',
'/examples/dbtng-example/update',
'/examples/dbtng-example/advanced',
],
];
}
public function testUi() {
$assert = $this
->assertSession();
$this
->drupalLogin($this
->createUser());
$this
->drupalGet('/examples/dbtng-example');
$assert
->statusCodeEquals(200);
$assert
->pageTextMatches('%John[td/<>\\w\\s]+Doe%');
$this
->drupalPostForm('/examples/dbtng-example/add', [
'name' => 'Some',
'surname' => 'Anonymous',
'age' => 33,
], 'Add');
$this
->drupalGet('/examples/dbtng-example');
$assert
->pageTextMatches('%Some[td/<>\\w\\s]+Anonymous%');
$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');
$this
->drupalGet('/examples/dbtng-example');
$assert
->pageTextMatches('%NewFirstName[td/<>\\w\\s]+Anonymous%');
$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());
$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');
}
}