Repository Access Links:

This URL has Read-Only access.
Statistics
| Branch: | Revision:

dolibarr-module-template / test / functional / MyModuleFunctionalTest.php @ ef37f43f

History | View | Annotate | Download (8.2 kB)

1
<?php
2
/* <one line to give the program's name and a brief idea of what it does.>
3
 * Copyright (C) <year>  <name of author>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

    
19
/**
20
 * \file    test/functional/MyModuleFunctionalTest.php
21
 * \ingroup mymodule
22
 * \brief   Example Selenium test.
23
 *
24
 * Put detailed description here.
25
 */
26

    
27
namespace test\functional;
28

    
29
use PHPUnit_Extensions_Selenium2TestCase_WebDriverException;
30

    
31
/**
32
 * Class MyModuleFunctionalTest
33
 *
34
 * Requires chromedriver for Google Chrome
35
 * Requires geckodriver for Mozilla Firefox
36
 *
37
 * @fixme Firefox (Geckodriver/Marionette) support
38
 * @todo Opera linux support
39
 * @todo Windows support (IE, Google Chrome, Mozilla Firefox, Safari)
40
 * @todo OSX support (Safari, Google Chrome, Mozilla Firefox)
41
 *
42
 * @package test\functional
43
 */
44
class MyModuleFunctionalTest extends \PHPUnit_Extensions_Selenium2TestCase
45
{
46
    // TODO: move to a global configuration file?
47
    /** @var string Base URL of the webserver under test */
48
    protected static $base_url = 'http://dev.zenfusion.fr';
49
    /**
50
     * @var string Dolibarr admin username
51
     * @see authenticate
52
     */
53
    protected static $dol_admin_user = 'admin';
54
    /**
55
     * @var string Dolibarr admin password
56
     * @see authenticate
57
     */
58
    protected static $dol_admin_pass = 'admin';
59
    /** @var int Dolibarr module ID */
60
    private static $module_id = 500000; // TODO: autodetect?
61

    
62
    /** @var array Browsers to test with */
63
    public static $browsers = array(
64
        array(
65
            'browser' => 'Google Chrome on Linux',
66
            'browserName' => 'chrome',
67
            'sessionStrategy' => 'shared',
68
            'desiredCapabilities' => array()
69
        ),
70
        // Geckodriver does not keep the session at the moment?!
71
        // XPath selectors also don't seem to work
72
//        array(
73
//            'browser' => 'Mozilla Firefox on Linux',
74
//            'browserName' => 'firefox',
75
//            'sessionStrategy' => 'shared',
76
//            'desiredCapabilities' => array(
77
//                'marionette' => true
78
//            )
79
//        )
80
    );
81

    
82
    /**
83
     * Helper function to select links by href
84
     *
85
     * @param $value
86
     * @return mixed
87
     */
88
    protected function byHref($value)
89
    {
90
        $anchor = null;
91
        $anchors = $this->elements($this->using('tag name')->value('a'));
92
        foreach ($anchors as $anchor) {
93
            if (strstr($anchor->attribute('href'), $value)) {
94
                break;
95
            }
96
        }
97
        return $anchor;
98
    }
99

    
100
    /**
101
     * Global test setup
102
     */
103
    public static function setUpBeforeClass()
104
    {
105
    }
106

    
107
    /**
108
     * Unit test setup
109
     */
110
    public function setUp()
111
    {
112
        $this->setSeleniumServerRequestsTimeout(3600);
113
        $this->setBrowserUrl(self::$base_url);
114
    }
115

    
116
    /**
117
     * Verify pre conditions
118
     */
119
    protected function assertPreConditions()
120
    {
121
    }
122

    
123
    /**
124
     * Handle Dolibarr authentication
125
     */
126
    private function authenticate()
127
    {
128
        try {
129
            if ($this->byId('login')) {
130
                $login = $this->byId('username');
131
                $login->clear();
132
                $login->value('admin');
133
                $password = $this->byId('password');
134
                $password->clear();
135
                $password->value('admin');
136
                $this->byId('login')->submit();
137
            }
138
        } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
139
            // Login does not exist. Assume we are already authenticated
140
        }
141
    }
142

    
143
    /**
144
     * Test enabling developer mode
145
     */
146
    public function testEnableDeveloperMode()
147
    {
148
        $this->url('/admin/const.php');
149
        $this->authenticate();
150
        $main_features_level_path='//input[@value="MAIN_FEATURES_LEVEL"]/following::input[@type="text"]';
151
        $main_features_level = $this->byXPath($main_features_level_path);
152
        $main_features_level->clear();
153
        $main_features_level->value('2');
154
        $this->byName('update')->click();
155
        // Page reloaded, we need a new XPath
156
        $main_features_level = $this->byXPath($main_features_level_path);
157
        return $this->assertEquals('2', $main_features_level->value(), "MAIN_FEATURES_LEVEL value is 2");
158
    }
159

    
160
    /**
161
     * Test enabling the module
162
     *
163
     * @depends testEnableDeveloperMode
164
     */
165
    public function testModuleEnabled()
166
    {
167
        $this->url('/admin/modules.php');
168
        $this->authenticate();
169
        $module_status_image_path='//a[contains(@href, "' . self::$module_id . '")]/img';
170
        $module_status_image = $this->byXPath($module_status_image_path);
171
        if (strstr($module_status_image->attribute('src'), 'switch_off.png')) {
172
            // Enable the module
173
            $this->byHref('modMyModule')->click();
174
        } else {
175
            // Disable the module
176
            $this->byHref('modMyModule')->click();
177
            // Reenable the module
178
            $this->byHref('modMyModule')->click();
179
        }
180
        // Page reloaded, we need a new Xpath
181
        $module_status_image = $this->byXPath($module_status_image_path);
182
        return $this->assertContains('switch_on.png', $module_status_image->attribute('src'), "Module enabled");
183
    }
184

    
185
    /**
186
     * Test access to the configuration page
187
     *
188
     * @depends testModuleEnabled
189
     */
190
    public function testConfigurationPage()
191
    {
192
        $this->url('/custom/mymodule/admin/setup.php');
193
        $this->authenticate();
194
        return $this->assertContains('mymodule/admin/setup.php', $this->url(), 'Configuration page');
195
    }
196

    
197
    /**
198
     * Test access to the about page
199
     *
200
     * @depends testConfigurationPage
201
     */
202
    public function testAboutPage()
203
    {
204
        $this->url('/custom/mymodule/admin/about.php');
205
        $this->authenticate();
206
        return $this->assertContains('mymodule/admin/about.php', $this->url(), 'About page');
207
    }
208

    
209
    /**
210
     * Test about page is rendering Markdown
211
     *
212
     * @depends testAboutPage
213
     */
214
    public function testAboutPageRendersMarkdownReadme()
215
    {
216
        $this->url('/custom/mymodule/admin/about.php');
217
        $this->authenticate();
218
        return $this->assertEquals(
219
            'Dolibarr Module Template (aka My Module)',
220
            $this->byTag('h1')->text(),
221
            "Readme title"
222
        );
223
    }
224

    
225
    /**
226
     * Test box is properly declared
227
     *
228
     * @depends testModuleEnabled
229
     */
230
    public function testBoxDeclared()
231
    {
232
        $this->url('/admin/boxes.php');
233
        $this->authenticate();
234
        return $this->assertContains('mybox', $this->source(), "Box enabled");
235
    }
236

    
237
    /**
238
     * Test trigger is properly enabled
239
     *
240
     * @depends testModuleEnabled
241
     */
242
    public function testTriggerDeclared()
243
    {
244
        $this->url('/admin/triggers.php');
245
        $this->authenticate();
246
        return $this->assertContains(
247
            'interface_99_modMyModule_MyTrigger.class.php',
248
            $this->byTag('body')->text(),
249
            "Trigger declared"
250
        );
251
    }
252

    
253
    /**
254
     * Test trigger is properly declared
255
     *
256
     * @depends testTriggerDeclared
257
     */
258
    public function testTriggerEnabled()
259
    {
260
        $this->url('/admin/triggers.php');
261
        $this->authenticate();
262
        return $this->assertContains(
263
            'tick.png',
264
            $this
265
                ->byXPath('//td[text()="interface_99_modMyModule_MyTrigger.class.php"]/following::img')
266
                ->attribute('src'),
267
            "Trigger enabled"
268
        );
269
    }
270

    
271
    /**
272
     * Verify post conditions
273
     */
274
    protected function assertPostConditions()
275
    {
276
    }
277

    
278
    /**
279
     * Unit test teardown
280
     */
281
    public function tearDown()
282
    {
283
    }
284

    
285
    /**
286
     * Global test teardown
287
     */
288
    public static function tearDownAfterClass()
289
    {
290
    }
291
}