View source
<?php
namespace Drupal\nodeorder\Tests;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\taxonomy\Tests\TaxonomyTestBase;
class NodeorderCrudTest extends TaxonomyTestBase {
public static $modules = [
'taxonomy',
'node',
'nodeorder',
];
protected $nodeOrderManager;
protected $field;
protected $vocabulary;
public function setUp() {
parent::setUp();
$this->nodeOrderManager = $this->container
->get('nodeorder.manager');
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
'bypass node access',
'order nodes within categories',
]));
$this->vocabulary = $this
->createVocabulary();
$field_name = 'taxonomy_' . $this->vocabulary
->id();
\Drupal::service('entity_type.manager')
->getStorage('field_storage_config')
->create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'entity_reference',
'module' => 'taxonomy',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'target_type' => 'taxonomy_term',
],
])
->save();
$this->field = \Drupal::service('entity_type.manager')
->getStorage('field_config')
->create([
'field_name' => $field_name,
'bundle' => 'article',
'entity_type' => 'node',
'settings' => [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
],
],
]);
$this->field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article', 'default')
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article', 'default')
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
}
public function testOrderableVocabulary() {
$this
->assertFalse($this->nodeOrderManager
->vocabularyIsOrderable($this->vocabulary
->id()), 'The test vocabulary is not orderable by default.');
\Drupal::configFactory()
->getEditable('nodeorder.settings')
->set('vocabularies', [
$this->vocabulary
->id() => TRUE,
])
->save();
$this
->assertTrue($this->nodeOrderManager
->vocabularyIsOrderable($this->vocabulary
->id()), 'The test vocabulary is orderable.');
}
public function testNodeCrudOperations() {
\Drupal::configFactory()
->getEditable('nodeorder.settings')
->set('vocabularies', [
$this->vocabulary
->id() => TRUE,
])
->save();
$term1 = $this
->createTerm($this->vocabulary);
$term2 = $this
->createTerm($this->vocabulary);
$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);
$expected = [
$node1
->id() => 0,
$node2
->id() => -1,
];
$this
->assertNodeorderByTid($term1
->id(), $expected);
$this
->assertNodeorderByTid($term2
->id(), $expected);
$edit = [
'entities[' . $node1
->id() . '][weight]' => -1,
'entities[' . $node2
->id() . '][weight]' => 1,
];
$this
->drupalPostForm('taxonomy/term/' . $term1
->id() . '/order', $edit, t('Save'));
$this
->assertNodeorderByTid($term2
->id(), $expected);
$expected = [
$node1
->id() => -1,
$node2
->id() => 1,
];
$this
->assertNodeorderByTid($term1
->id(), $expected);
$node3 = [
'type' => 'article',
'title' => 'cccc',
$this->field
->getName() => [
[
'target_id' => $term1
->id(),
],
[
'target_id' => $term2
->id(),
],
],
];
$node3 = $this
->drupalCreateNode($node3);
$expected[$node3
->id()] = -2;
$this
->assertNodeorderByTid($term1
->id(), $expected);
$expected = [
$node1
->id() => 0,
$node2
->id() => -1,
$node3
->id() => -2,
];
$this
->assertNodeorderByTid($term2
->id(), $expected);
$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'));
$this
->assertNodeorderByTid($term2
->id(), $expected);
$expected = [
$node1
->id() => -2,
$node2
->id() => 1,
$node3
->id() => 2,
];
$this
->assertNodeorderByTid($term1
->id(), $expected);
$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);
$node4 = [
'type' => 'article',
'title' => 'dddd',
$this->field
->getName() => [
[
'target_id' => $term1
->id(),
],
[
'target_id' => $term2
->id(),
],
],
];
$node4 = $this
->drupalCreateNode($node4);
$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);
$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'));
$this
->assertNodeorderByTid($term2
->id(), $expected);
$expected = [
$node1
->id() => -2,
$node3
->id() => 1,
$node4
->id() => 2,
];
$this
->assertNodeorderByTid($term1
->id(), $expected);
$node3->{$this->field
->getName()} = [
[
'target_id' => $term2
->id(),
],
];
$node3
->save();
$expected = [
$node1
->id() => -1,
$node4
->id() => 0,
];
$this
->assertNodeorderByTid($term1
->id(), $expected);
$expected = [
$node1
->id() => 0,
$node3
->id() => -1,
$node4
->id() => -2,
];
$this
->assertNodeorderByTid($term2
->id(), $expected);
}
protected function assertNodeorderByTid($tid, array $expected) {
$order = \Drupal::database()
->select('taxonomy_index', 'ti')
->fields('ti', [
'nid',
'weight',
])
->condition('tid', $tid)
->execute()
->fetchAllAssoc('nid');
foreach ($order as $nid => $row) {
if (isset($expected[$nid]) && $expected[$nid] == $row->weight) {
unset($expected[$nid]);
unset($order[$nid]);
}
}
if (!empty($expected) || !empty($order)) {
debug($expected, 'Orderings that did not match.');
debug($order, 'Orderings found that were not expected.');
$this
->fail('Order did not match.');
}
else {
$this
->pass('Order of nodes matched.');
}
}
}