You are here

public function ReorderChildrenFunctionalTest::testReordering in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ReorderChildrenFunctionalTest.php \Drupal\Tests\entity_hierarchy\Functional\ReorderChildrenFunctionalTest::testReordering()

Tests children reorder form.

File

tests/src/Functional/ReorderChildrenFunctionalTest.php, line 58

Class

ReorderChildrenFunctionalTest
Defines a class for testing the reorder children form.

Namespace

Drupal\Tests\entity_hierarchy\Functional

Code

public function testReordering() {
  $entities = $this
    ->createChildEntities($this->parent
    ->id());
  $root_node = $this->treeStorage
    ->getNode($this->parentStub);
  $children = $this->treeStorage
    ->findChildren($root_node
    ->getNodeKey());
  $this
    ->assertCount(5, $children);
  $this
    ->assertEquals(array_map(function ($name) use ($entities) {
    return $entities[$name]
      ->id();
  }, [
    'Child 5',
    'Child 4',
    'Child 3',
    'Child 2',
    'Child 1',
  ]), array_map(function (Node $node) {
    return $node
      ->getId();
  }, $children));

  // Now insert one in the middle.
  $name = 'Child 6';
  $entities[$name] = $this
    ->createTestEntity($this->parent
    ->id(), $name, -2);
  $children = $this->treeStorage
    ->findChildren($root_node
    ->getNodeKey());
  $this
    ->assertCount(6, $children);
  $this
    ->assertEquals(array_map(function ($name) use ($entities) {
    return $entities[$name]
      ->id();
  }, [
    'Child 5',
    'Child 4',
    'Child 3',
    'Child 2',
    'Child 6',
    'Child 1',
  ]), array_map(function (Node $node) {
    return $node
      ->getId();
  }, $children));

  // Now we visit the form for reordering.
  $this
    ->drupalGet($this->parent
    ->toUrl('entity_hierarchy_reorder'));
  $assert = $this
    ->assertSession();

  // Access denied.
  $assert
    ->statusCodeEquals(403);

  // Now login.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'reorder entity_hierarchy children',
    'view test entity',
    'administer entity_test content',
  ]));
  $this
    ->drupalGet($this->parent
    ->toUrl('entity_hierarchy_reorder'));
  $assert
    ->statusCodeEquals(200);
  foreach ($entities as $entity) {
    $assert
      ->linkExists($entity
      ->label());
  }
  $assert
    ->pageTextContains('Type');
  $assert
    ->pageTextContains('Entity Test Bundle');

  // Now move Child 6 to the top.
  $this
    ->submitForm([
    'children[' . $entities[$name]
      ->id() . '][weight]' => -10,
  ], 'Update child order');
  $children = $this->treeStorage
    ->findChildren($root_node
    ->getNodeKey());
  $this
    ->assertCount(6, $children);
  $this
    ->assertEquals(array_map(function ($name) use ($entities) {
    return $entities[$name]
      ->id();
  }, [
    'Child 6',
    'Child 5',
    'Child 4',
    'Child 3',
    'Child 2',
    'Child 1',
  ]), array_map(function (Node $node) {
    return $node
      ->getId();
  }, $children));
  $this
    ->drupalGet($this->parent
    ->toUrl());
  $assert
    ->linkExists('Children');
  $different_test_entity = EntityTestRev::create([
    'type' => 'entity_test_rev',
    'label' => 'No children here',
  ]);
  $different_test_entity
    ->save();
  $this
    ->drupalGet($different_test_entity
    ->toUrl());
  $assert
    ->linkNotExists('Children');
  $this
    ->drupalGet($different_test_entity
    ->toUrl('entity_hierarchy_reorder'));

  // No field, should be not found here.
  $assert
    ->statusCodeEquals(403);

  // Add a new bundle.
  entity_test_create_bundle('someotherbundle');
  $another_different_test_entity = EntityTest::create([
    'type' => 'someotherbundle',
    'label' => 'No children here either',
  ]);
  $another_different_test_entity
    ->save();
  $this
    ->drupalGet($another_different_test_entity
    ->toUrl());

  // Link should show, because entity is valid target bundle.
  $assert
    ->linkExists('Children');
  $this
    ->drupalGet($another_different_test_entity
    ->toUrl('entity_hierarchy_reorder'));
  $assert
    ->statusCodeEquals(200);

  // Now edit the field and disable referencing someotherbundle.
  $field = FieldConfig::load('entity_test.entity_test.parents');
  $settings = $field
    ->getSetting('handler_settings');
  $settings['target_bundles'] = [
    'entity_test',
  ];
  $field
    ->setSetting('handler_settings', $settings);
  $field
    ->save();
  $another_different_test_entity = EntityTest::create([
    'type' => 'someotherbundle',
    'label' => 'No children here either',
  ]);
  $another_different_test_entity
    ->save();
  $this
    ->drupalGet($another_different_test_entity
    ->toUrl());
  $assert
    ->linkNotExists('Children');
  $this
    ->drupalGet($another_different_test_entity
    ->toUrl('entity_hierarchy_reorder'));

  // No field, should be not found here.
  $assert
    ->statusCodeEquals(403);
}