Skip to content
Snippets Groups Projects
Commit e01f0543 authored by Bojan Zivanovic's avatar Bojan Zivanovic
Browse files

Add tests.

parent d14056f6
No related branches found
No related tags found
No related merge requests found
entity.entity_test_enhanced.collection:
path: '/entity_test_enhanced'
defaults:
_entity_list: 'entity_test_enhanced'
_title: 'Entity test with enhancements'
requirements:
_permission: 'administer entity_test_enhanced'
......@@ -32,6 +32,7 @@ use Drupal\entity\Revision\RevisionableContentEntityBase;
* "create" = "\Drupal\entity\Routing\CreateHtmlRouteProvider",
* "delete-multiple" = "\Drupal\entity\Routing\DeleteMultipleRouteProvider",
* },
* "list_builder" = "\Drupal\Core\Entity\EntityListBuilder",
* },
* base_table = "entity_test_enhanced",
* data_table = "entity_test_enhanced_field_data",
......@@ -50,6 +51,7 @@ use Drupal\entity\Revision\RevisionableContentEntityBase;
* "add-page" = "/entity_test_enhanced/add",
* "add-form" = "/entity_test_enhanced/add/{type}",
* "canonical" = "/entity_test_enhanced/{entity_test_enhanced}",
* "collection" = "/entity_test_enhanced",
* "delete-multiple-form" = "/entity_test_enhanced/delete",
* "revision" = "/entity_test_enhanced/{entity_test_enhanced}/revisions/{entity_test_enhanced_revision}/view",
* "revision-revert-form" = "/entity_test_enhanced/{entity_test_enhanced}/revisions/{entity_test_enhanced_revision}/revert",
......
<?php
/**
* @file
* Contains \Drupal\Tests\entity\Functional\DeleteMultipleFormTest.
*/
namespace Drupal\Tests\entity\Functional;
use Drupal\entity_module_test\Entity\EnhancedEntity;
use Drupal\entity_module_test\Entity\EnhancedEntityBundle;
use Drupal\simpletest\BrowserTestBase;
/**
* Tests the delete multiple confirmation form.
*
* @group entity
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class DeleteMultipleFormTest extends BrowserTestBase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface;
*/
protected $account;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['entity_module_test', 'user', 'entity'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
EnhancedEntityBundle::create([
'id' => 'default',
'label' => 'Default',
])->save();
$this->account = $this->drupalCreateUser(['administer entity_test_enhanced']);
$this->drupalLogin($this->account);
}
/**
* Tests the add page.
*/
public function testForm() {
$entities = [];
$selection = [];
for ($i = 0; $i < 2; $i++) {
$entity = EnhancedEntity::create([
'type' => 'default',
]);
$entity->save();
$entities[$entity->id()] = $entity;
$langcode = $entity->language()->getId();
$selection[$entity->id()][$langcode] = $langcode;
}
// Add the selection to the tempstore just like DeleteAction would.
$tempstore = \Drupal::service('user.private_tempstore')->get('entity_delete_multiple_confirm');
$tempstore->set($this->account->id(), $selection);
$this->drupalGet('/entity_test_enhanced/delete');
$assert = $this->assertSession();
$assert->statusCodeEquals(200);
$assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these items?');
$delete_button = $this->getSession()->getPage()->findButton('Delete');
$delete_button->click();
$assert = $this->assertSession();
$assert->addressEquals('/entity_test_enhanced');
$assert->responseContains('Deleted 2 items.');
\Drupal::entityTypeManager()->getStorage('entity_test_enhanced')->resetCache();
$remaining_entities = EnhancedEntity::loadMultiple(array_keys($selection));
$this->assertEmpty($remaining_entities);
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\entity\Kernel\DeleteActionTest.
*/
namespace Drupal\Tests\entity\Kernel;
use Drupal\entity\Plugin\Action\DeleteAction;
use Drupal\entity_module_test\Entity\EnhancedEntity;
use Drupal\entity_module_test\Entity\EnhancedEntityBundle;
use Drupal\system\Entity\Action;
use Drupal\user\Entity\User;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the delete entity action.
*/
class DeleteActionTest extends KernelTestBase {
/**
* The current user.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
public static $modules = ['action', 'node', 'entity_module_test', 'entity',
'user', 'system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test_enhanced');
$this->installSchema('system', ['key_value_expire', 'sequences']);
$bundle = EnhancedEntityBundle::create([
'id' => 'default',
'label' => 'Default',
]);
$bundle->save();
$this->user = User::create([
'name' => 'username',
'status' => 1,
]);
$this->user->save();
\Drupal::service('current_user')->setAccount($this->user);
}
public function testAction() {
/** @var \Drupal\system\ActionConfigEntityInterface $action */
$action = Action::create([
'id' => 'enhanced_entity_delete_action',
'label' => 'Delete enhanced entity',
'plugin' => 'entity_delete_action:entity_test_enhanced',
]);
$status = $action->save();
$this->assertEquals(SAVED_NEW, $status);
$this->assertInstanceOf(DeleteAction::class, $action->getPlugin());
$entities = [];
for ($i = 0; $i < 2; $i++) {
$entity = EnhancedEntity::create([
'type' => 'default',
]);
$entity->save();
$entities[$entity->id()] = $entity;
}
$action->execute($entities);
// Confirm that the entity ids and langcodes are now in the tempstore.
$tempstore = \Drupal::service('user.private_tempstore')->get('entity_delete_multiple_confirm');
$selection = $tempstore->get($this->user->id());
$this->assertEquals(array_keys($entities), array_keys($selection));
$this->assertEquals([['en' => 'en'], ['en' => 'en']], array_values($selection));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment