3 namespace Drupal\Tests\Core\Common;
5 use Drupal\Component\Utility\Tags;
6 use Drupal\Tests\UnitTestCase;
9 * Tests explosion and implosion of autocomplete tags.
13 class TagsTest extends UnitTestCase {
15 protected $validTags = [
17 'Drupal with some spaces' => 'Drupal with some spaces',
18 '"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"',
19 '"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!',
23 * Explodes a series of tags.
25 public function explodeTags() {
26 $string = implode(', ', array_keys($this->validTags));
27 $tags = Tags::explode($string);
28 $this->assertTags($tags);
32 * Implodes a series of tags.
34 public function testImplodeTags() {
35 $tags = array_values($this->validTags);
36 // Let's explode and implode to our heart's content.
37 for ($i = 0; $i < 10; $i++) {
38 $string = Tags::implode($tags);
39 $tags = Tags::explode($string);
41 $this->assertTags($tags);
45 * Helper function: asserts that the ending array of tags is what we wanted.
47 protected function assertTags($tags) {
48 $original = $this->validTags;
49 foreach ($tags as $tag) {
50 $key = array_search($tag, $original);
51 $this->assertTrue((bool) $key, $tag, sprintf('Make sure tag %s shows up in the final tags array (originally %s)', $tag, $key));
52 unset($original[$key]);
54 foreach ($original as $leftover) {
55 $this->fail(sprintf('Leftover tag %s was left over.', $leftover));