More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Common / TagsTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Common;
4
5 use Drupal\Component\Utility\Tags;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests explosion and implosion of autocomplete tags.
10  *
11  * @group Common
12  */
13 class TagsTest extends UnitTestCase {
14
15   protected $validTags = [
16     'Drupal' => 'Drupal',
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!',
20   ];
21
22   /**
23    * Explodes a series of tags.
24    */
25   public function explodeTags() {
26     $string = implode(', ', array_keys($this->validTags));
27     $tags = Tags::explode($string);
28     $this->assertTags($tags);
29   }
30
31   /**
32    * Implodes a series of tags.
33    */
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);
40     }
41     $this->assertTags($tags);
42   }
43
44   /**
45    * Helper function: asserts that the ending array of tags is what we wanted.
46    */
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]);
53     }
54     foreach ($original as $leftover) {
55       $this->fail(sprintf('Leftover tag %s was left over.', $leftover));
56     }
57   }
58
59 }