dolibarr 20.0.2
MyModuleFunctionalTest.php
1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) ---Put here your own copyright and developer email---
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 <https://www.gnu.org/licenses/>.
17 */
18
27namespace Dolibarr\test\functional;
28
29use PHPUnit_Extensions_Selenium2TestCase_WebDriverException;
30
45{
46 // TODO: move to a global configuration file?
48 protected static $base_url = 'http://dev.zenfusion.fr';
53 protected static $dol_admin_user = 'admin';
58 protected static $dol_admin_pass = 'admin';
60 private static $module_id = 500000; // TODO: autodetect?
61
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
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
104 public static function setUpBeforeClass()
105 {
106 }
107
112 public function setUp()
113 {
114 $this->setSeleniumServerRequestsTimeout(3600);
115 $this->setBrowserUrl(self::$base_url);
116 }
117
122 protected function assertPreConditions()
123 {
124 }
125
130 private function authenticate()
131 {
132 try {
133 if ($this->byId('login')) {
134 $login = $this->byId('username');
135 $login->clear();
136 $login->value('admin');
137 $password = $this->byId('password');
138 $password->clear();
139 $password->value('admin');
140 $this->byId('login')->submit();
141 }
142 } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
143 // Login does not exist. Assume we are already authenticated
144 }
145 }
146
151 public function testEnableDeveloperMode()
152 {
153 $this->url('/admin/const.php');
154 $this->authenticate();
155 $main_features_level_path = '//input[@value="MAIN_FEATURES_LEVEL"]/following::input[@type="text"]';
156 $main_features_level = $this->byXPath($main_features_level_path);
157 $main_features_level->clear();
158 $main_features_level->value('2');
159 $this->byName('update')->click();
160 // Page reloaded, we need a new XPath
161 $main_features_level = $this->byXPath($main_features_level_path);
162 return $this->assertEquals('2', $main_features_level->value(), "MAIN_FEATURES_LEVEL value is 2");
163 }
164
171 public function testModuleEnabled()
172 {
173 $this->url('/admin/modules.php');
174 $this->authenticate();
175 $module_status_image_path = '//a[contains(@href, "'.self::$module_id.'")]/img';
176 $module_status_image = $this->byXPath($module_status_image_path);
177 if (strstr($module_status_image->attribute('src'), 'switch_off.png')) {
178 // Enable the module
179 $this->byHref('modMyModule')->click();
180 } else {
181 // Disable the module
182 $this->byHref('modMyModule')->click();
183 // Re-enable the module
184 $this->byHref('modMyModule')->click();
185 }
186 // Page reloaded, we need a new Xpath
187 $module_status_image = $this->byXPath($module_status_image_path);
188 return $this->assertContains('switch_on.png', $module_status_image->attribute('src'), "Module enabled");
189 }
190
197 public function testConfigurationPage()
198 {
199 $this->url('/custom/mymodule/admin/setup.php');
200 $this->authenticate();
201 return $this->assertContains('mymodule/admin/setup.php', $this->url(), 'Configuration page');
202 }
203
210 public function testAboutPage()
211 {
212 $this->url('/custom/mymodule/admin/about.php');
213 $this->authenticate();
214 return $this->assertContains('mymodule/admin/about.php', $this->url(), 'About page');
215 }
216
224 {
225 $this->url('/custom/mymodule/admin/about.php');
226 $this->authenticate();
227 return $this->assertEquals(
228 'Dolibarr Module Template (aka My Module)',
229 $this->byTag('h1')->text(),
230 "Readme title"
231 );
232 }
233
240 public function testBoxDeclared()
241 {
242 $this->url('/admin/boxes.php');
243 $this->authenticate();
244 return $this->assertContains('mymodulewidget1', $this->source(), "Box enabled");
245 }
246
253 public function testTriggerDeclared()
254 {
255 $this->url('/admin/triggers.php');
256 $this->authenticate();
257 return $this->assertContains(
258 'interface_99_modMyModule_MyModuleTriggers.class.php',
259 $this->byTag('body')->text(),
260 "Trigger declared"
261 );
262 }
263
270 public function testTriggerEnabled()
271 {
272 $this->url('/admin/triggers.php');
273 $this->authenticate();
274 return $this->assertContains(
275 'tick.png',
276 $this->byXPath('//td[text()="interface_99_modMyModule_MyTrigger.class.php"]/following::img')->attribute('src'),
277 "Trigger enabled"
278 );
279 }
280
285 protected function assertPostConditions()
286 {
287 }
288
293 public function tearDown()
294 {
295 }
296
301 public static function tearDownAfterClass()
302 {
303 }
304}
testConfigurationPage()
Test access to the configuration page.
byHref($value)
Helper function to select links by href.
testTriggerDeclared()
Test trigger is properly enabled.
testAboutPageRendersMarkdownReadme()
Test about page is rendering Markdown.
testTriggerEnabled()
Test trigger is properly declared.