Passed
Push — dev ( 3becbd...634f81 )
by Rafael
53:25
created

MyModuleFunctionalTest::testTriggerDeclared()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2007-2017 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) ---Put here your own copyright and developer email---
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace Dolibarr\test\functional;
21
22
use PHPUnit_Extensions_Selenium2TestCase_WebDriverException;
23
24
/**
25
 * \file    test/phpunit/MyModuleFunctionalTest.php
26
 * \ingroup mymodule
27
 * \brief   Example Selenium test.
28
 *
29
 * Put detailed description here.
30
 */
31
32
/**
33
 * Class MyModuleFunctionalTest
34
 *
35
 * Requires chromedriver for Google Chrome
36
 * Requires geckodriver for Mozilla Firefox
37
 *
38
 * @fixme Firefox (Geckodriver/Marionette) support
39
 * @todo Opera linux support
40
 * @todo Windows support (IE, Google Chrome, Mozilla Firefox, Safari)
41
 * @todo OSX support (Safari, Google Chrome, Mozilla Firefox)
42
 *
43
 * @package Testmymodule
44
 */
45
class MyModuleFunctionalTest extends \PHPUnit_Extensions_Selenium2TestCase
46
{
47
    // TODO: move to a global configuration file?
48
    /** @var string Base URL of the webserver under test */
49
    protected static $base_url = 'http://dev.zenfusion.fr';
50
    /**
51
     * @var string Dolibarr admin username
52
     * @see authenticate()
53
     */
54
    protected static $dol_admin_user = 'admin';
55
    /**
56
     * @var string Dolibarr admin password
57
     * @see authenticate()
58
     */
59
    protected static $dol_admin_pass = 'admin';
60
    /** @var int Dolibarr module ID */
61
    private static $module_id = 500000; // TODO: autodetect?
62
63
    /** @var array Browsers to test with */
64
    public static $browsers = array(
65
        array(
66
            'browser' => 'Google Chrome on Linux',
67
            'browserName' => 'chrome',
68
            'sessionStrategy' => 'shared',
69
            'desiredCapabilities' => array()
70
        ),
71
        // Geckodriver does not keep the session at the moment?!
72
        // XPath selectors also don't seem to work
73
        //array(
74
        //    'browser' => 'Mozilla Firefox on Linux',
75
        //    'browserName' => 'firefox',
76
        //    'sessionStrategy' => 'shared',
77
        //    'desiredCapabilities' => array(
78
        //        'marionette' => true,
79
        //    ),
80
        //)
81
    );
82
83
    /**
84
     * Helper function to select links by href
85
     *
86
     * @param  string  $value      Href
87
     * @return mixed               Helper string
88
     */
89
    protected function byHref($value)
90
    {
91
        $anchor = null;
92
        $anchors = $this->elements($this->using('tag name')->value('a'));
93
        foreach ($anchors as $anchor) {
94
            if (strstr($anchor->attribute('href'), $value)) {
95
                break;
96
            }
97
        }
98
        return $anchor;
99
    }
100
101
    /**
102
     * Global test setup
103
     * @return void
104
     */
105
    public static function setUpBeforeClass()
106
    {
107
    }
108
109
    /**
110
     * Unit test setup
111
     * @return void
112
     */
113
    public function setUp()
114
    {
115
        $this->setSeleniumServerRequestsTimeout(3600);
116
        $this->setBrowserUrl(self::$base_url);
117
    }
118
119
    /**
120
     * Verify pre conditions
121
     * @return void
122
     */
123
    protected function assertPreConditions()
124
    {
125
    }
126
127
    /**
128
     * Handle Dolibarr authentication
129
     * @return void
130
     */
131
    private function authenticate()
132
    {
133
        try {
134
            if ($this->byId('login')) {
135
                $login = $this->byId('username');
136
                $login->clear();
137
                $login->value('admin');
138
                $password = $this->byId('password');
139
                $password->clear();
140
                $password->value('admin');
141
                $this->byId('login')->submit();
142
            }
143
        } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
144
            // Login does not exist. Assume we are already authenticated
145
        }
146
    }
147
148
    /**
149
     * Test enabling developer mode
150
     * @return bool
151
     */
152
    public function testEnableDeveloperMode()
153
    {
154
        $this->url('/admin/const.php');
155
        $this->authenticate();
156
        $main_features_level_path = '//input[@value="MAIN_FEATURES_LEVEL"]/following::input[@type="text"]';
157
        $main_features_level = $this->byXPath($main_features_level_path);
158
        $main_features_level->clear();
159
        $main_features_level->value('2');
160
        $this->byName('update')->click();
161
        // Page reloaded, we need a new XPath
162
        $main_features_level = $this->byXPath($main_features_level_path);
163
        return $this->assertEquals('2', $main_features_level->value(), "MAIN_FEATURES_LEVEL value is 2");
164
    }
165
166
    /**
167
     * Test enabling the module
168
     *
169
     * @depends testEnableDeveloperMode
170
     * @return bool
171
     */
172
    public function testModuleEnabled()
173
    {
174
        $this->url('/admin/modules.php');
175
        $this->authenticate();
176
        $module_status_image_path = '//a[contains(@href, "' . self::$module_id . '")]/img';
177
        $module_status_image = $this->byXPath($module_status_image_path);
178
        if (strstr($module_status_image->attribute('src'), 'switch_off.png')) {
179
            // Enable the module
180
            $this->byHref('modMyModule')->click();
181
        } else {
182
            // Disable the module
183
            $this->byHref('modMyModule')->click();
184
            // Re-enable the module
185
            $this->byHref('modMyModule')->click();
186
        }
187
        // Page reloaded, we need a new Xpath
188
        $module_status_image = $this->byXPath($module_status_image_path);
189
        return $this->assertContains('switch_on.png', $module_status_image->attribute('src'), "Module enabled");
190
    }
191
192
    /**
193
     * Test access to the configuration page
194
     *
195
     * @depends testModuleEnabled
196
     * @return bool
197
     */
198
    public function testConfigurationPage()
199
    {
200
        $this->url('/custom/mymodule/admin/setup.php');
201
        $this->authenticate();
202
        return $this->assertContains('mymodule/admin/setup.php', $this->url(), 'Configuration page');
203
    }
204
205
    /**
206
     * Test access to the about page
207
     *
208
     * @depends testConfigurationPage
209
     * @return bool
210
     */
211
    public function testAboutPage()
212
    {
213
        $this->url('/custom/mymodule/admin/about.php');
214
        $this->authenticate();
215
        return $this->assertContains('mymodule/admin/about.php', $this->url(), 'About page');
216
    }
217
218
    /**
219
     * Test about page is rendering Markdown
220
     *
221
     * @depends testAboutPage
222
     * @return bool
223
     */
224
    public function testAboutPageRendersMarkdownReadme()
225
    {
226
        $this->url('/custom/mymodule/admin/about.php');
227
        $this->authenticate();
228
        return $this->assertEquals(
229
            'Dolibarr Module Template (aka My Module)',
230
            $this->byTag('h1')->text(),
231
            "Readme title"
232
        );
233
    }
234
235
    /**
236
     * Test box is properly declared
237
     *
238
     * @depends testModuleEnabled
239
     * @return bool
240
     */
241
    public function testBoxDeclared()
242
    {
243
        $this->url('/admin/boxes.php');
244
        $this->authenticate();
245
        return $this->assertContains('mymodulewidget1', $this->source(), "Box enabled");
246
    }
247
248
    /**
249
     * Test trigger is properly enabled
250
     *
251
     * @depends testModuleEnabled
252
     * @return bool
253
     */
254
    public function testTriggerDeclared()
255
    {
256
        $this->url('/admin/triggers.php');
257
        $this->authenticate();
258
        return $this->assertContains(
259
            'interface_99_modMyModule_MyModuleTriggers.class.php',
260
            $this->byTag('body')->text(),
261
            "Trigger declared"
262
        );
263
    }
264
265
    /**
266
     * Test trigger is properly declared
267
     *
268
     * @depends testTriggerDeclared
269
     * @return bool
270
     */
271
    public function testTriggerEnabled()
272
    {
273
        $this->url('/admin/triggers.php');
274
        $this->authenticate();
275
        return $this->assertContains(
276
            'tick.png',
277
            $this->byXPath('//td[text()="interface_99_modMyModule_MyTrigger.class.php"]/following::img')->attribute('src'),
278
            "Trigger enabled"
279
        );
280
    }
281
282
    /**
283
     * Verify post conditions
284
     * @return void
285
     */
286
    protected function assertPostConditions()
287
    {
288
    }
289
290
    /**
291
     * Unit test teardown
292
     * @return void
293
     */
294
    public function tearDown()
295
    {
296
    }
297
298
    /**
299
     * Global test teardown
300
     * @return void
301
     */
302
    public static function tearDownAfterClass()
303
    {
304
    }
305
}
306