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

MobileMessagingTest.php « Integration « tests « MobileMessaging « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f387534bf30d6442dfbb10d46fe321182ca801c8 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\MobileMessaging\tests\Integration;

use Piwik\Access;
use Piwik\Plugins\MobileMessaging\API as APIMobileMessaging;
use Piwik\Plugins\MobileMessaging\MobileMessaging;
use Piwik\Plugins\MobileMessaging\SMSProvider;
use Piwik\Plugins\ScheduledReports\API as APIScheduledReports;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Tests\Impl\IntegrationTestCase;
use FakeAccess;

/**
 * Class Plugins_MobileMessagingTest
 *
 * @group Plugins
 */
class MobileMessagingTest extends IntegrationTestCase
{
    protected $idSiteAccess;

    public function setUp()
    {
        parent::setUp();

        // setup the access layer
        $pseudoMockAccess = new FakeAccess;
        FakeAccess::$superUser = true;
        //finally we set the user as a Super User by default
        Access::setSingletonInstance($pseudoMockAccess);

        $this->idSiteAccess = APISitesManager::getInstance()->addSite("test", "http://test");

        \Piwik\Plugin\Manager::getInstance()->loadPlugins(array('ScheduledReports', 'MobileMessaging', 'MultiSites'));
        \Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
    }

    /**
     * When the MultiSites plugin is not activated, the SMS content should invite the user to activate it back
     *
     * @group Plugins
     */
    public function testWarnUserViaSMSMultiSitesDeactivated()
    {
        // safety net
        \Piwik\Plugin\Manager::getInstance()->loadPlugins(array('ScheduledReports', 'MobileMessaging'));
        $this->assertFalse(\Piwik\Plugin\Manager::getInstance()->isPluginActivated('MultiSites'));

        $APIScheduledReports = APIScheduledReports::getInstance();
        $reportId = $APIScheduledReports->addReport(
            $this->idSiteAccess,
            'description',
            'month',
            0,
            'mobile',
            'sms',
            array(),
            array("phoneNumbers" => array('33698896656'))
        );

        list($outputFilename, $prettyDate, $websiteName, $additionalFiles) =
            $APIScheduledReports->generateReport(
                $reportId,
                '01-01-2010',
                'en',
                2
            );

        $handle = fopen($outputFilename, "r");
        $contents = fread($handle, filesize($outputFilename));
        fclose($handle);

        $this->assertEquals(
            \Piwik\Piwik::translate('MobileMessaging_MultiSites_Must_Be_Activated'),
            $contents
        );
    }

    /**
     * Dataprovider for testTruncate
     */
    public function getTruncateTestCases()
    {

        $stdGSMx459 = str_repeat('a', 459);

        $extGSMx229 = str_repeat('€', 229);

        $alternatedGSMx153 = str_repeat('a€', 153);

        $GSMWithRegExpSpecialChars = $stdGSMx459 . '[\^$.|?*/+()';

        $UCS2x201 = str_repeat('控', 201);

        // appended strings
        $stdGSMAppendedString = 'too long';
        $extGSMAppendedString = '[too long]';
        $UCS2AppendedString = '[控控]';

        return array(

            // maximum number of standard GSM characters
            array($stdGSMx459, $stdGSMx459, 3, 'N/A'),

            // maximum number of extended GSM characters
            array($extGSMx229, $extGSMx229, 3, 'N/A'),

            // maximum number of alternated GSM characters
            array($alternatedGSMx153, $alternatedGSMx153, 3, 'N/A'),

            // standard GSM, one 'a' too many, appended with standard GSM characters
            array(str_repeat('a', 451) . $stdGSMAppendedString, $stdGSMx459 . 'a', 3, $stdGSMAppendedString),

            // standard GSM, one 'a' too many, appended with extended GSM characters
            array(str_repeat('a', 447) . $extGSMAppendedString, $stdGSMx459 . 'a', 3, $extGSMAppendedString),

            // standard GSM, one 'a' too many, appended with UCS2 characters
            array(str_repeat('a', 197) . $UCS2AppendedString, $stdGSMx459 . 'a', 3, $UCS2AppendedString),

            // extended GSM, one '€' too many, appended with standard GSM characters
            array(str_repeat('€', 225) . $stdGSMAppendedString, $extGSMx229 . '€', 3, $stdGSMAppendedString),

            // extended GSM, one '€' too many, appended with extended GSM characters
            array(str_repeat('€', 223) . $extGSMAppendedString, $extGSMx229 . '€', 3, $extGSMAppendedString),

            // extended GSM, one '€' too many, appended with UCS2 characters
            array(str_repeat('€', 197) . $UCS2AppendedString, $extGSMx229 . '€', 3, $UCS2AppendedString),

            // alternated GSM, one 'a' too many, appended with standard GSM characters
            array(str_repeat('a€', 150) . 'a' . $stdGSMAppendedString, $alternatedGSMx153 . 'a', 3, $stdGSMAppendedString),

            // alternated GSM, one 'a' too many, appended with extended GSM characters
            array(str_repeat('a€', 149) . $extGSMAppendedString, $alternatedGSMx153 . 'a', 3, $extGSMAppendedString),

            // alternated GSM, one 'a' too many, appended with UCS2 characters
            array(str_repeat('a€', 98) . 'a' . $UCS2AppendedString, $alternatedGSMx153 . 'a', 3, $UCS2AppendedString),

            // alternated GSM, one '€' too many, appended with standard GSM characters
            array(str_repeat('a€', 150) . 'a' . $stdGSMAppendedString, $alternatedGSMx153 . '€', 3, $stdGSMAppendedString),

            // alternated GSM, one '€' too many, appended with extended GSM characters
            array(str_repeat('a€', 149) . $extGSMAppendedString, $alternatedGSMx153 . '€', 3, $extGSMAppendedString),

            // alternated GSM, one '€' too many, appended with UCS2 characters
            array(str_repeat('a€', 98) . 'a' . $UCS2AppendedString, $alternatedGSMx153 . '€', 3, $UCS2AppendedString),

            // GSM with RegExp reserved special chars
            array(str_repeat('a', 451) . $stdGSMAppendedString, $GSMWithRegExpSpecialChars, 3, $stdGSMAppendedString),

            // maximum number of UCS-2 characters
            array($UCS2x201, $UCS2x201, 3, 'N/A'),

            // UCS-2, one '控' too many, appended with UCS2 characters
            array(str_repeat('控', 197) . $UCS2AppendedString, $UCS2x201 . '控', 3, $UCS2AppendedString),

            // UCS-2, one '控' too many, appended with standard GSM characters
            array(str_repeat('控', 193) . $stdGSMAppendedString, $UCS2x201 . '控', 3, $stdGSMAppendedString)
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider getTruncateTestCases
     */
    public function testTruncate($expected, $stringToTruncate, $maximumNumberOfConcatenatedSMS, $appendedString)
    {
        $this->assertEquals(
            $expected,
            SMSProvider::truncate($stringToTruncate, $maximumNumberOfConcatenatedSMS, $appendedString)
        );
    }

    /**
     * Dataprovider for testContainsUCS2Characters
     */
    public function getContainsUCS2CharactersTestCases()
    {
        return array(
            array(false, 'too long'),
            array(false, '[too long]'),
            array(false, '€'),
            array(true, '[控控]'),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider getContainsUCS2CharactersTestCases
     */
    public function testContainsUCS2Characters($expected, $stringToTest)
    {
        $this->assertEquals(
            $expected,
            SMSProvider::containsUCS2Characters($stringToTest)
        );
    }

    /**
     * @group Plugins
     */
    public function testSanitizePhoneNumber()
    {
        $this->assertEquals('676932647', APIMobileMessaging::sanitizePhoneNumber('  6  76 93 26 47'));
    }

    /**
     * @group Plugins
     */
    public function testPhoneNumberIsSanitized()
    {
        $mobileMessagingAPI = APIMobileMessaging::getInstance();
        $mobileMessagingAPI->setSMSAPICredential('StubbedProvider', '');
        $mobileMessagingAPI->addPhoneNumber('  6  76 93 26 47');
        $this->assertEquals('676932647', key($mobileMessagingAPI->getPhoneNumbers()));
    }

    /**
     * Dataprovider for testSendReport
     */
    public function getSendReportTestCases()
    {
        return array(
            array('reportContent', '0101010101', 'Piwik.org', 'reportContent', '0101010101', 'Piwik.org'),
            array('reportContent', '0101010101', 'General_Reports', 'reportContent', '0101010101', 'General_MultiSitesSummary'),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider getSendReportTestCases
     */
    public function testSendReport($expectedReportContent, $expectedPhoneNumber, $expectedFrom, $reportContent, $phoneNumber, $reportSubject)
    {
        $report = array(
            'parameters' => array(MobileMessaging::PHONE_NUMBERS_PARAMETER => array($phoneNumber)),
        );

        $stubbedAPIMobileMessaging = $this->getMock('\\Piwik\\Plugins\\MobileMessaging\\API', array('sendSMS', 'getInstance'), $arguments = array(), $mockClassName = '', $callOriginalConstructor = false);
        $stubbedAPIMobileMessaging->expects($this->once())->method('sendSMS')->with(
            $this->equalTo($expectedReportContent, 0),
            $this->equalTo($expectedPhoneNumber, 1),
            $this->equalTo($expectedFrom, 2)
        );

        \Piwik\Plugins\MobileMessaging\API::setSingletonInstance($stubbedAPIMobileMessaging);

        $mobileMessaging = new MobileMessaging();
        $mobileMessaging->sendReport(MobileMessaging::MOBILE_TYPE, $report, $reportContent, null, null, $reportSubject, null, null, null, false);

        \Piwik\Plugins\MobileMessaging\API::unsetInstance();
    }
}