devel_wizard-2.x-dev/templates/spell/entity_type/config/test.functional-javascript.crud.php.twig
templates/spell/entity_type/config/test.functional-javascript.crud.php.twig
<?php
declare(strict_types=1);
namespace Drupal\Tests\{{ module.machineName }}\FunctionalJavascript;
use Behat\Mink\Element\DocumentElement;
class ConfigEntityCrudTest extends TestBase {
/**
* @throws \Throwable
*/
public function testCrud(): void {
$admin = $this->createUser(
[
'access administration pages',
'view the administration theme',
'{{ module.machineName }}.{{ config.id }}.admin',
],
);
$this->drupalLogin($admin);
$values = [
'label' => 'My type 01',
'id' => 'my_type_01',
'description' => 'My desc 01',
'help' => 'My help 01',
'weight' => 4,
];
$this->crudCreate($values);
$old = $values;
$values['label'] .= ' - edit 01';
$values['description'] .= ' - edit 01';
$values['help'] .= ' - edit 01';
$values['weight'] += 10;
$this->crudEdit($old, $values);
$this->crudDelete($values);
}
/**
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ResponseTextException
*/
protected function crudCreate(array $values): static {
$this->drupalGet('/admin/structure/{{ config.id }}');
$this->clickLink('Add {{ config.label }}');
$page = $this->getSession()->getPage();
$this->fillConfigEntityForm($page, $values, 'add');
$page->pressButton('Save');
$assert = $this->assertSession();
$assert->pageTextContains(sprintf(
'"%s" {{ config.label }} has been created',
$values['label'],
));
return $this;
}
/**
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ResponseTextException
*/
protected function crudEdit(array $old, array $new): static {
$this->drupalGet("/admin/structure/{{ config.id }}/manage/{$new['id']}");
$assert = $this->assertSession();
$assert->fieldValueEquals('label', $old['label']);
$assert->fieldValueEquals('description', $old['description']);
$assert->fieldValueEquals('help', $old['help']);
$assert->fieldValueEquals('weight', (string) $old['weight']);
$page = $this->getSession()->getPage();
$this->fillConfigEntityForm($page, $new, 'edit');
$page->pressButton('Save');
$assert = $this->assertSession();
$assert->pageTextContains(sprintf(
'"%s" {{ config.label }} has been updated',
$new['label'],
));
return $this;
}
/**
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ResponseTextException
*/
protected function crudDelete(array $values): static {
$this->drupalGet("/admin/structure/{{ config.id }}/manage/{$values['id']}");
$page = $this->getSession()->getPage();
$page->clickLink('Delete');
// @todo Find a better way to wait for the confirmation DialogUI.
sleep(3);
$assert = $this->assertSession();
$assert->pageTextContains(sprintf(
'Are you sure you want to delete the {{ config.label }} %s?',
$values['label'],
));
// @todo This used to work $page->pressButton('Delete'), but not with DialogUI.
$this
->getSession()
->getPage()
->find('css', '.ui-dialog-buttonset.form-actions .button--primary')
->click();
$assert = $this->assertSession();
$assert->pageTextContains(sprintf(
'The {{ config.label }} %s has been deleted.',
$values['label'],
));
return $this;
}
/**
* @throws \Behat\Mink\Exception\ElementNotFoundException
*/
protected function fillConfigEntityForm(DocumentElement $page, array $values, string $type): static {
if (array_key_exists('label', $values)) {
$page->fillField('Label', $values['label']);
}
if (array_key_exists('id', $values) && $type === 'add') {
$page->waitFor(
2,
function() {
return $this
->getSession()
->getPage()
->find('css', 'button[aria-label="Edit machine name"]');
},
);
$page = $this->getSession()->getPage();
$page
->find('css', 'button[aria-label="Edit machine name"]')
->press();
$page->fillField('id', $values['id']);
}
if (array_key_exists('description', $values)) {
$page->fillField('Description', $values['description']);
}
if (array_key_exists('help', $values)) {
$page->fillField('Help', $values['help']);
}
if (array_key_exists('weight', $values)) {
$page->fillField('Weight', (string) $values['weight']);
}
return $this;
}
}
