CancelButtonSettingsFormTest.php in Entity Form Cancel Button 8
File
tests/src/Functional/CancelButtonSettingsFormTest.php
View source
<?php
namespace Drupal\Tests\cancel_button\Functional;
use Drupal\Tests\BrowserTestBase;
class CancelButtonSettingsFormTest extends BrowserTestBase {
public static $modules = [
'cancel_button',
'node',
];
protected $adminUser;
protected $translationManager;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->createUser([
'access administration pages',
'administer site configuration',
'administer cancel button configuration',
]);
$this->translationManager = \Drupal::translation();
$this
->drupalLogin($this->adminUser);
}
public function testCancelButtonDefaultDestinations() {
$this
->drupalGet('admin/config/content/cancel-button');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('default_cancel_destination');
$edit = [
'default_cancel_destination' => 'y&^blP3',
];
$this
->drupalPostForm('admin/config/content/cancel-button', $edit, t('Save configuration'));
$this
->assertSession()
->responseContains($this->translationManager
->translate("The path '%path' is either invalid or you do not have access to it.", [
'%path' => $edit['default_cancel_destination'],
]));
$entity_types = $this->container
->get('entity_type.manager')
->getDefinitions();
$bundles_by_entity = [];
$final_edit = [];
foreach ($entity_types as $entity_type_id => $entity_type) {
if (array_key_exists('wizard', $entity_type
->getHandlerClasses())) {
continue;
}
if ($entity_type
->hasKey('bundle')) {
$bundle_entity_type = $entity_type
->getBundleEntityType();
if (!empty($bundle_entity_type)) {
$bundles_by_entity[$entity_type_id] = $this->container
->get('entity_type.manager')
->getStorage($bundle_entity_type)
->loadMultiple();
}
}
if (!isset($bundles_by_entity[$entity_type_id]) || count($bundles_by_entity[$entity_type_id]) == 0) {
$final_edit = array_merge($final_edit, [
$entity_type_id . '_cancel_destination' => '/admin',
]);
}
}
foreach ($bundles_by_entity as $entity_type_id => $bundles) {
foreach ($bundles as $bundle) {
$final_edit = array_merge($final_edit, [
$entity_type_id . '_' . $bundle
->id() . '_cancel_destination' => '/admin',
]);
}
}
$final_edit = array_merge($final_edit, [
'default_cancel_destination' => '/admin',
]);
$this
->drupalPostForm('admin/config/content/cancel-button', $final_edit, t('Save configuration'));
$this
->assertSession()
->pageTextContains(t('The configuration options have been saved.'));
}
}