1<?php
2
3/**
4 * Test emoji unicode substitution
5 */
6class emoji_unicode_test extends DokuWikiTest {
7
8    function setUp() {
9        $this->pluginsEnabled[] = 'emoji';
10        parent::setUp();
11    }
12
13    function test_implicittext() {
14        $instructions = p_get_instructions("x"
15                                          ."\xC2\xA9"
16                                          ."\xE2\x84\xA2"
17                                          ."\xE2\x96\xB6"
18                                          ."\xE3\x80\xB0"
19                                          ."x");
20        $foundemoji = $this->count_array_values('emoji', $this->flatten_array($instructions), true);
21        $this->assertEquals(0, $foundemoji, 'Emoji failed implicit text.');
22    }
23
24    function test_implicitemoji() {
25        $instructions = p_get_instructions("x"
26                                          ."0\xE2\x83\xA3"
27                                          ."\xE2\x8C\x9A"
28                                          ."\xE2\x9D\x8C"
29                                          ."\xE2\xAD\x90"
30                                          ."\xF0\x9F\x87\xA9\xF0\x9F\x87\xBC"
31                                          ."\xF0\x9F\x8C\xA0"
32                                          ."x");
33        $foundemoji = $this->count_array_values('emoji', $this->flatten_array($instructions));
34        $this->assertEquals(6, $foundemoji, 'Emoji failed implicit emoji.');
35    }
36
37    function test_explicittext() {
38        $instructions = p_get_instructions("x"
39                                          ."0\xE2\x83\xA3\xEF\xB8\x8E"
40                                          ."\xE2\x8C\x9A\xEF\xB8\x8E"
41                                          ."\xE2\x9D\x8C\xEF\xB8\x8E"
42                                          ."\xE2\xAD\x90\xEF\xB8\x8E"
43                                          ."\xF0\x9F\x8C\xA0\xEF\xB8\x8E"
44                                          ."x");
45        $foundemoji = $this->count_array_values('emoji', $this->flatten_array($instructions), true);
46        $this->assertEquals(0, $foundemoji, 'Emoji failed explicit text.');
47    }
48
49    function test_explicitemoji() {
50        $instructions = p_get_instructions("x"
51                                          ."#\xEF\xB8\x8F"
52                                          ."\xC2\xA9\xEF\xB8\x8F"
53                                          ."\xE2\x84\xA2\xEF\xB8\x8F"
54                                          ."\xE2\x96\xB6\xEF\xB8\x8F"
55                                          ."\xE3\x80\xB0\xEF\xB8\x8F"
56                                          ."x");
57        $foundemoji = $this->count_array_values('emoji', $this->flatten_array($instructions));
58        $this->assertEquals(5, $foundemoji, 'Emoji failed explicit emoji.');
59    }
60
61    function flatten_array($array) {
62        $array = array_values($array);
63        $return = array();
64        while($array) {
65            $value = array_shift($array);
66            if(is_array($value)) {
67                array_splice($array, 0, 0, $value);
68            } else {
69                $return[] = $value;
70            }
71        }
72        return $return;
73    }
74
75    function count_array_values($value, $array) {
76        $count = 0;
77        foreach($array as $item) {
78            if($item === $value) ++$count;
79        }
80        return $count;
81    }
82}
83