FormModeManagerRouteTest.php in Form mode manager 8.2
File
tests/src/Functional/FormModeManagerRouteTest.php
View source
<?php
namespace Drupal\Tests\form_mode_manager\Functional;
use Drupal\user\Entity\Role;
class FormModeManagerRouteTest extends FormModeManagerBase {
protected $node1;
public function testAnonymousSpecificFormModeManagerRoutes() {
$node_form_mode_id = $this->formModeManager
->getFormModeMachineName($this->nodeFormMode
->id());
$node_type_test_page = $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();
$this
->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/{$node_form_mode_id}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("node/add/{$this->nodeTypeFmm1->id()}");
$this
->assertSession()
->statusCodeEquals(403);
$this->node1 = $this
->drupalCreateNode([
'title' => 'Test node',
'type' => $this->nodeTypeFmm1
->id(),
]);
$this
->drupalGet("node/{$this->node1->id()}/edit/{$node_form_mode_id}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("node/{$this->node1->id()}/edit");
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet("node/add/test_page");
$this
->assertSession()
->statusCodeEquals(403);
$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);
}
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);
$this
->drupalLogin($this->anonymousUser);
$this
->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/not-valid-fm");
$this
->assertSession()
->statusCodeEquals(404);
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);
$this
->drupalGet("node/add/{$this->nodeTypeFmm1->id()}/{$node_form_mode_id}");
$this
->assertSession()
->statusCodeEquals(403);
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);
}
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);
$node_form_mode_id = $this->formModeManager
->getFormModeMachineName($this->nodeFormMode
->id());
$this
->drupalGet("node/{$this->node1->id()}/edit/not-valid-fm");
$this
->assertSession()
->statusCodeEquals(404);
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);
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);
}
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);
$user_form_mode_id = $this->formModeManager
->getFormModeMachineName($this->userFormMode
->id());
$this
->drupalGet("user/{$this->adminUser->id()}/edit/not-valid-fm");
$this
->assertSession()
->statusCodeEquals(404);
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);
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);
}
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);
$this
->drupalLogin($this->anonymousUser);
$this
->drupalGet("node/add-list/{$node_form_mode_id}");
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("node/add-list/not-valid-fm");
$this
->assertSession()
->statusCodeEquals(404);
}
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);
$this
->drupalGet("admin/structure/types/manage/{$node_type_fmm2->id()}/form-display");
$edit = [
"display_modes_custom[{$node_form_mode_id}]" => TRUE,
];
$this
->drupalPostForm("admin/structure/types/manage/{$node_type_fmm2->id()}/form-display", $edit, t('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);
$this
->drupalGet("node/add-list/not-valid-fm");
$this
->assertSession()
->statusCodeEquals(404);
}
}