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->label()} 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);
  }
  
  public function testFormModeManagerPlugin($entity_type_id, $operation, $route_name_expected) {
    
    $route_properties_plugin = \Drupal::service('plugin.manager.entity_routing_map')
      ->createInstance($entity_type_id, [
      'entityTypeId' => $entity_type_id,
    ]);
    $this
      ->assertEquals($route_name_expected, $route_properties_plugin
      ->getOperation($operation), 'Operation ' . $operation . ' correctly retrieved for' . $entity_type_id . ' entity.');
  }
  
  public function providerEntityRouteInfos() {
    $data = [];
    $data[] = [
      'node',
      'add_form',
      'node.add',
    ];
    $data[] = [
      'node',
      'edit_form',
      'entity.node.edit_form',
    ];
    $data[] = [
      'user',
      'add_form',
      'user.register',
    ];
    $data[] = [
      'user',
      'edit_form',
      'entity.user.edit_form',
    ];
    $data[] = [
      'user',
      'admin_add',
      'user.admin_create',
    ];
    $data[] = [
      'block_content',
      'add_form',
      'block_content.add_form',
    ];
    $data[] = [
      'block_content',
      'edit_form',
      'entity.block_content.edit_form',
    ];
    $data[] = [
      'taxonomy_term',
      'add_form',
      'entity.taxonomy_term.add_form',
    ];
    $data[] = [
      'taxonomy_term',
      'edit_form',
      'entity.taxonomy_term.edit_form',
    ];
    $data[] = [
      'media',
      'add_form',
      'entity.media.add_form',
    ];
    $data[] = [
      'media',
      'edit_form',
      'entity.media.edit_form',
    ];
    return $data;
  }
  
  public function testAdminRoutes($route_name) {
    $route = \Drupal::service('router.route_provider')
      ->getRouteByName($route_name);
    $is_admin = \Drupal::service('router.admin_context')
      ->isAdminRoute($route);
    $this
      ->assertTrue($is_admin, 'Admin route correctly marked for "Form Mode Manager settings" pages.');
  }
  
  public function providerAdminRoutes() {
    $data = [];
    $data[] = [
      'form_mode_manager.admin_settings',
    ];
    $data[] = [
      'form_mode_manager.admin_settings_links_task',
    ];
    return $data;
  }
}