Automatically re-order tags on save

Fixes #167
This commit is contained in:
Oliver Davies 2020-08-28 11:35:54 +01:00
parent 5e107b48d6
commit 59a98b8d78
13 changed files with 268 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\Tests\opdavies_blog\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
final class ReorderBlogTagsTest extends EntityKernelTestBase {
use NodeCreationTrait;
use TaxonomyTestTrait;
public static $modules = [
// Core.
'node',
'taxonomy',
'link',
// Contrib.
'discoverable_entity_bundle_classes',
'hook_event_dispatcher',
// Custom.
'opdavies_blog_test',
'opdavies_blog',
];
/** @test */
public function it_reorders_tags_on_blog_posts_to_be_arranged_alphabetically(): void {
/** @var VocabularyInterface $vocabulary */
$vocabulary = Vocabulary::load('tags');
$this->createTerm($vocabulary, ['name' => 'Drupal']); // 1
$this->createTerm($vocabulary, ['name' => 'PHP']); // 2
$this->createTerm($vocabulary, ['name' => 'Symfony']); // 3
$post = $this->createNode([
'field_tags' => [3, 1, 2],
'type' => 'post',
]);
/** @var Post $post */
$post = Node::load($post->id());
$this->assertSame(
['Drupal', 'PHP', 'Symfony'],
$post->getTags()
->map(fn(TermInterface $tag) => $tag->label())
->toArray()
);
}
protected function setUp() {
parent::setUp();
$this->installConfig([
'filter',
'opdavies_blog_test',
]);
$this->installEntitySchema('taxonomy_vocabulary');
$this->installEntitySchema('taxonomy_term');
}
}