Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TranslationWriterTest.php « Core « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27a4fa597a2a2522a2d4752de5cf2bd6f29913c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
class TranslationWriterTest extends PHPUnit_Framework_TestCase
{
    /**
     * Dataprovider for testClean
     */
    public function getCleanTestData()
    {
        return array(
            // empty string
            array("", ''),
            // newline
            array("\n", ''),
            // leading and trailing whitespace
            array(" a \n", 'a'),
            // single / double quotes
            array(" &quot;it&#039;s&quot; ", '"it\'s"'),
            // html special characters
            array("&lt;tag&gt;", '<tag>'),
            // other html entities
            array("&hellip;", '…'),
        );
    }

    /**
     * @group Core
     * @group TranslationWriter
     * @dataProvider getCleanTestData
     */
    public function testClean($data, $expected)
    {
        $this->assertEquals($expected, Piwik_TranslationWriter::clean($data));
    }

    /**
     * Dataprovider for testQuote
     */
    public function getQuoteTestData()
    {
        return array(
            // alphanumeric
            array('abc 123', "'abc 123'"),
            // newline
            array("\n", "'\n'"),
            // tab
            array('	', "'	'"),
            // single quote
            array("it's", "'it\'s'"),
        );
    }

    /**
     * @group Core
     * @group TranslationWriter
     * @dataProvider getQuoteTestData
     */
    public function testQuote($data, $expected)
    {
        if (Piwik_Common::isWindows() && $data == "\n") {
            return;
        }
        $this->assertEquals($expected, Piwik_TranslationWriter::quote($data));
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testGetTranslationPathInvalidLang()
    {
        try {
            $path = Piwik_TranslationWriter::getTranslationPath('../index');
        } catch (Exception $e) {
            return;
        }
        $this->fail('Expected exception not raised');
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testGetTranslationPathInvalidBasePath()
    {
        try {
            $path = Piwik_TranslationWriter::getTranslationPath('en', 'core');
        } catch (Exception $e) {
            return;
        }
        $this->fail('Expected exception not raised');
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testGetTranslationPath()
    {
        // implicit base path
        $this->assertEquals(PIWIK_INCLUDE_PATH . '/lang/en.php', Piwik_TranslationWriter::getTranslationPath('en'));

        // explicit base path
        $this->assertEquals(PIWIK_INCLUDE_PATH . '/lang/en.php', Piwik_TranslationWriter::getTranslationPath('en', 'lang'));
        $this->assertEquals(PIWIK_INCLUDE_PATH . '/tmp/en.php', Piwik_TranslationWriter::getTranslationPath('en', 'tmp'));
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testLoadTranslationInvalidLang()
    {
        try {
            $translations = Piwik_TranslationWriter::loadTranslation('a');
        } catch (Exception $e) {
            return;
        }
        $this->fail('Expected exception not raised');
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testLoadTranslation()
    {
        require PIWIK_INCLUDE_PATH . '/lang/en.php';
        $this->assertTrue(is_array($translations));

        $englishTranslations = Piwik_TranslationWriter::loadTranslation('en');

        $this->assertEquals(count($translations), count($englishTranslations));
        $this->assertEquals(0, count(array_diff($translations, $englishTranslations)));
        $this->assertEquals(0, count(array_diff_assoc($translations, $englishTranslations)));
    }

    /**
     * @group Core
     * @group TranslationWriter
     */
    public function testSaveTranslation()
    {
        $path = Piwik_TranslationWriter::getTranslationPath('en', 'tmp');

        $translations = array(
            'General_Locale' => 'en_CA.UTF-8',
            'General_Id'     => 'Id',
            'Goals_Goals'    => 'Goals',
            'Plugin_Body'    => "Message\nBody",
        );

        @unlink($path);

        $rc = Piwik_TranslationWriter::saveTranslation($translations, $path);
        $this->assertNotEquals(false, $rc);

        $contents = file_get_contents($path);
        $expected = "<?php\n\$translations = array(\n\t'General_Locale' => 'en_CA.UTF-8',\n\t'General_Id' => 'Id',\n\t'Goals_Goals' => 'Goals',\n\n\t// FOR REVIEW\n\t'Plugin_Body' => 'Message\nBody',\n);\n";
        if (Piwik_Common::isWindows()) $expected = str_replace("\r\n", "\n", $expected);
        $this->assertEquals($expected, $contents);
    }
}