You are here

public function NodeorderCrudTest::testNodeCrudOperations in Node Order 8

Test node CRUD operations that nodeorder depends on.

File

src/Tests/NodeorderCrudTest.php, line 117

Class

NodeorderCrudTest
Test CRUD operations that Nodeorder relies on.

Namespace

Drupal\nodeorder\Tests

Code

public function testNodeCrudOperations() {

  // Enable 'orderable' on this vocabulary.
  \Drupal::configFactory()
    ->getEditable('nodeorder.settings')
    ->set('vocabularies', [
    $this->vocabulary
      ->id() => TRUE,
  ])
    ->save();

  // Create two taxonomy terms.
  $term1 = $this
    ->createTerm($this->vocabulary);
  $term2 = $this
    ->createTerm($this->vocabulary);

  // Create several nodes in both terms.
  $node1 = [
    'type' => 'article',
    'title' => 'aaaa',
    $this->field
      ->getName() => [
      [
        'target_id' => $term1
          ->id(),
      ],
      [
        'target_id' => $term2
          ->id(),
      ],
    ],
  ];
  $node1 = $this
    ->drupalCreateNode($node1);
  $node2 = [
    'type' => 'article',
    'title' => 'bbbb',
    $this->field
      ->getName() => [
      [
        'target_id' => $term1
          ->id(),
      ],
      [
        'target_id' => $term2
          ->id(),
      ],
    ],
  ];
  $node2 = $this
    ->drupalCreateNode($node2);

  // Initial order should be identical in each term.
  $expected = [
    $node1
      ->id() => 0,
    $node2
      ->id() => -1,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Re-order nodes in term 1.
  $edit = [
    'entities[' . $node1
      ->id() . '][weight]' => -1,
    'entities[' . $node2
      ->id() . '][weight]' => 1,
  ];
  $this
    ->drupalPostForm('taxonomy/term/' . $term1
    ->id() . '/order', $edit, t('Save'));

  // Order in term 2 should remain unchanged.
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Verify order in term 1.
  $expected = [
    $node1
      ->id() => -1,
    $node2
      ->id() => 1,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);

  // Add a third node.
  $node3 = [
    'type' => 'article',
    'title' => 'cccc',
    $this->field
      ->getName() => [
      [
        'target_id' => $term1
          ->id(),
      ],
      [
        'target_id' => $term2
          ->id(),
      ],
    ],
  ];
  $node3 = $this
    ->drupalCreateNode($node3);

  // New node should be at the very top in both terms.
  $expected[$node3
    ->id()] = -2;
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);
  $expected = [
    $node1
      ->id() => 0,
    $node2
      ->id() => -1,
    $node3
      ->id() => -2,
  ];
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Move node 1 back to the top in term 1.
  $edit = [
    'entities[' . $node1
      ->id() . '][weight]' => -2,
    'entities[' . $node2
      ->id() . '][weight]' => 1,
    'entities[' . $node3
      ->id() . '][weight]' => 2,
  ];
  $this
    ->drupalPostForm('taxonomy/term/' . $term1
    ->id() . '/order', $edit, t('Save'));

  // Order in term 2 should remain the same.
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Verify order in term 1.
  $expected = [
    $node1
      ->id() => -2,
    $node2
      ->id() => 1,
    $node3
      ->id() => 2,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);

  // Delete a node and verify orderings.
  $node2
    ->delete();
  $expected = [
    $node1
      ->id() => -1,
    $node3
      ->id() => 0,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);
  $expected = [
    $node1
      ->id() => 0,
    $node3
      ->id() => -1,
  ];
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Add a new node.
  $node4 = [
    'type' => 'article',
    'title' => 'dddd',
    $this->field
      ->getName() => [
      [
        'target_id' => $term1
          ->id(),
      ],
      [
        'target_id' => $term2
          ->id(),
      ],
    ],
  ];
  $node4 = $this
    ->drupalCreateNode($node4);

  // New node should be at the top of each term.
  $expected = [
    $node1
      ->id() => -1,
    $node3
      ->id() => 0,
    $node4
      ->id() => -2,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);
  $expected = [
    $node1
      ->id() => 0,
    $node3
      ->id() => -1,
    $node4
      ->id() => -2,
  ];
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Move node 1 back to the top in term 1.
  $edit = [
    'entities[' . $node1
      ->id() . '][weight]' => -2,
    'entities[' . $node3
      ->id() . '][weight]' => 1,
    'entities[' . $node4
      ->id() . '][weight]' => 2,
  ];
  $this
    ->drupalPostForm('taxonomy/term/' . $term1
    ->id() . '/order', $edit, t('Save'));

  // Order in term 2 should remain the same.
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);

  // Verify order in term 1.
  $expected = [
    $node1
      ->id() => -2,
    $node3
      ->id() => 1,
    $node4
      ->id() => 2,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);

  // Remove term 1 from node 3.
  $node3->{$this->field
    ->getName()} = [
    [
      'target_id' => $term2
        ->id(),
    ],
  ];
  $node3
    ->save();

  // Verify order in term 1.
  $expected = [
    $node1
      ->id() => -1,
    $node4
      ->id() => 0,
  ];
  $this
    ->assertNodeorderByTid($term1
    ->id(), $expected);

  // Term 2 ordering should remain unchanged.
  $expected = [
    $node1
      ->id() => 0,
    $node3
      ->id() => -1,
    $node4
      ->id() => -2,
  ];
  $this
    ->assertNodeorderByTid($term2
    ->id(), $expected);
}