You are here

FormModeManagerRouteTest.php in Form mode manager 8

Same filename and directory in other branches
  1. 8.2 tests/src/Functional/FormModeManagerRouteTest.php

File

tests/src/Functional/FormModeManagerRouteTest.php
View source
<?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.
   */
  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();

    // Add a node with custum 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 custum form mode.
    $this->node1 = $this
      ->drupalCreateNode([
      'title' => 'Test node',
      'type' => $this->nodeTypeFmm1
        ->id(),
    ]);

    // Edit node  with custum 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.
   */
  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 juste permission 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.
   */
  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.
   */
  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.
   */
  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);

    // 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.
   */
  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");
    $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);

    // List form mode not exit.
    $this
      ->drupalGet("node/add-list/not-valid-fm");
    $this
      ->assertSession()
      ->statusCodeEquals(404);
  }

  /**
   * Tests the Plugin to retrieve dynamically the entity route name.
   *
   * @param string $entity_type_id
   *   The name of entity type id to test.
   * @param string $operation
   *   The entity form operation name to test.
   * @param string $route_name_expected
   *   The route name expected.
   *
   * @dataProvider providerEntityRouteInfos
   */
  public function testFormModeManagerPlugin($entity_type_id, $operation, $route_name_expected) {

    /** @var \Drupal\form_mode_manager\EntityRoutingMapBase $route_properties_plugin */
    $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.');
  }

  /**
   * Provides test data for testAccess().
   *
   * @see \Drupal\Tests\form_mode_manager\Functional\FormModeManagerRouteTest::testFormModeManagerPlugin()
   */
  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;
  }

  /**
   * Asserts that admin routes are correctly marked as such.
   *
   * @param string $route_name
   *   The route name expected.
   *
   * @dataProvider providerAdminRoutes
   */
  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.');
  }

  /**
   * Provides test data for testAdminRoutes().
   *
   * @see \Drupal\Tests\form_mode_manager\Functional\FormModeManagerRouteTest::testAdminRoutes()
   */
  public function providerAdminRoutes() {
    $data = [];
    $data[] = [
      'form_mode_manager.admin_settings',
    ];
    $data[] = [
      'form_mode_manager.admin_settings_links_task',
    ];
    return $data;
  }

}

Classes

Namesort descending Description
FormModeManagerRouteTest Tests the routes generated by form_mode_manager.