form_mode_manager-8.x-1.x-dev/tests/src/Functional/FormModeManagerRouteTest.php
tests/src/Functional/FormModeManagerRouteTest.php
<?php
namespace Drupal\Tests\form_mode_manager\Functional;
use Drupal\user\Entity\Role;
/**
* Tests the routes generated by form_mode_manager.
*
* @group form_mode_manager
*/
class FormModeManagerRouteTest extends FormModeManagerBase {
/**
* An user with administrative permissions.
*
* @var \Drupal\user\UserInterface
*/
protected $node1;
/**
* Asserts that anon had access to a specific form mode, create and edit node.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testAnonymousSpecificFormModeManagerRoutes() {
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$this->drupalCreateContentType([
'type' => 'test_page',
'name' => 'Test Page',
]);
$this->setUpFormMode("admin/structure/types/manage/test_page/form-display", $this->nodeFormMode->id());
$this->drupalLogin($this->anonymousUser);
Role::load($this->anonymousUser->getRoles()[1])
->grantPermission("use {$this->nodeFormMode->id()} form mode")
->grantPermission("create {$this->nodeTypeFmm1->id()} content")
->grantPermission("edit own {$this->nodeTypeFmm1->id()} content")
->save();
// Add a node with custom form mode.
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
// Add a node with default form mode not available.
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}");
$this->assertSession()->statusCodeEquals(403);
// Add a node with custom form mode.
$this->node1 = $this->drupalCreateNode([
'title' => 'Test node',
'type' => $this->nodeTypeFmm1->id(),
]);
// Edit node with custom form mode.
$this->drupalGet("node/{$this->node1->id()}/edit/{$node_form_mode_id}");
$this->assertSession()->statusCodeEquals(200);
// Edit a node with default form mode not available.
$this->drupalGet("node/{$this->node1->id()}/edit");
$this->assertSession()->statusCodeEquals(403);
// Make sure we can't add test_page content as this "anonymous" user.
$this->drupalGet("node/add/test_page");
$this->assertSession()->statusCodeEquals(403);
// Even if using a specific form mode.
$this->drupalGet("node/add/test_page/{$node_form_mode_id}");
$this->assertSession()->statusCodeEquals(403);
$this->drupalLogin($this->rootUser);
$this->drupalGet("node/add/test_page");
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet("node/add/test_page/{$node_form_mode_id}");
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts Add Form Mode Manager routes exists.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testAddFormModeManagerRoutes() {
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$this->drupalLogin($this->adminUser);
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
$this->drupalLogin($this->anonymousUser);
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// Test not found form mode add.
$this->drupalLogin($this->anonymousUser);
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
// Test add with needed permissions create xxx content.
Role::load($this->testUser->getRoles()[1])
->grantPermission("create {$this->nodeTypeFmm1->id()} content")
->grantPermission("use node.default form mode")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}");
$this->assertSession()->statusCodeEquals(200);
// Test add content with form mode without permission use xxx form mode.
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// Test add content with form mode with permission use xxx form mode.
Role::load($this->testUser->getRoles()[1])
->grantPermission("use {$this->nodeFormMode->id()} form mode")
->save();
$this->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts Edit Form Mode Manager routes exists.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testEditFormModeManagerRoutes() {
$this->drupalLogin($this->adminUser);
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$this->node1 = $this->drupalCreateNode([
'title' => 'Form Mode Manager content test',
'type' => $this->nodeTypeFmm1->id(),
]);
$this->drupalGet("node/{$this->node1->id()}/edit/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
// Test not found form mode edit.
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$this->drupalGet("node/{$this->node1->id()}/edit/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
// Test with just permission create xxx content.
Role::load($this->testUser->getRoles()[1])
->grantPermission("create {$this->nodeTypeFmm1->id()} content")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("node/{$this->node1->id()}/edit/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// Test with permission edit any xxx content and use xxx form mode.
Role::load($this->testUser->getRoles()[1])
->grantPermission("edit any {$this->nodeTypeFmm1->id()} content")
->grantPermission("use {$this->nodeFormMode->id()} form mode")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("node/{$this->node1->id()}/edit/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts User Edit Form Mode Manager routes exists.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testUserEditFormModeManagerRoutes() {
$this->drupalLogin($this->adminUser);
$user_form_mode_id = $this->formModeManager->getFormModeMachineName($this->userFormMode->id());
$this->drupalGet("user/{$this->rootUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
// Test not found form mode edit.
$user_form_mode_id = $this->formModeManager->getFormModeMachineName($this->userFormMode->id());
$this->drupalGet("user/{$this->adminUser->id()}/edit/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
// Test with just permission create xxx content.
Role::load($this->testUser->getRoles()[1])
->grantPermission("administer users")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("user/{$this->testUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// Test with permission edit any xxx content and use xxx form mode.
Role::load($this->testUser->getRoles()[1])
->grantPermission("use {$this->userFormMode->id()} form mode")
->save();
$this->drupalLogin($this->testUser);
$this->drupalGet("user/{$this->adminUser->id()}/edit/$user_form_mode_id");
$this->assertSession()->statusCodeEquals(200);
}
/**
* Asserts List With One Form Mode Manager routes exists.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testListWithOneFormModeManagerRoutes() {
$this->drupalLogin($this->adminUser);
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$this->drupalGet("node/add-list/$node_form_mode_id");
$this->assertSession()->titleEquals("Create {$this->nodeTypeFmm1->id()} as {$this->nodeFormMode->label()} | Drupal");
$this->assertSession()->statusCodeEquals(200);
// List form mode for anonymous.
$this->drupalLogin($this->anonymousUser);
$this->drupalGet("node/add-list/$node_form_mode_id");
$this->assertSession()->statusCodeEquals(403);
// List form mode not exit.
$this->drupalLogin($this->adminUser);
$this->drupalGet("node/add-list/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
}
/**
* Asserts List With Two Form Mode Manager routes exists.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testListWithTwoFormModeManagerRoutes() {
$node_form_mode_id = $this->formModeManager->getFormModeMachineName($this->nodeFormMode->id());
$node_type_fmm2 = $this->drupalCreateContentType([
'name' => 'Form Mode Manager Test 2',
'type' => 'fmm_test2',
]);
Role::load($this->adminUser->getRoles()[1])
->grantPermission('create ' . $node_type_fmm2->id() . ' content')
->grantPermission('edit any ' . $node_type_fmm2->id() . ' content')
->save();
$this->drupalLogin($this->adminUser);
// Add a content type and attach it to form mode.
$this->drupalGet("admin/structure/types/manage/{$node_type_fmm2->id()}/form-display");
$this->assertSession()->statusCodeEquals(200);
$edit = ["display_modes_custom[$node_form_mode_id]" => TRUE];
$this->drupalGet("admin/structure/types/manage/{$node_type_fmm2->id()}/form-display");
$this->submitForm($edit, 'Save');
$this->drupalGet("node/add-list/$node_form_mode_id");
$this->assertSession()->linkExists($this->nodeTypeFmm1->label());
$this->assertSession()->linkExists($node_type_fmm2->label());
$this->assertSession()->statusCodeEquals(200);
// List form mode not exit.
$this->drupalGet("node/add-list/not-valid-fm");
$this->assertSession()->statusCodeEquals(404);
}
}
