dolibarr 21.0.0-beta
MyModuleFunctionalTest.php
1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) ---Replace with 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
45class MyModuleFunctionalTest extends \PHPUnit_Extensions_Selenium2TestCase // @phan-suppress-current-line PhanUndeclaredExtendedClass
46{
47 // TODO: move to a global configuration file?
49 protected static $base_url = 'http://dev.zenfusion.fr';
54 protected static $dol_admin_user = 'admin';
59 protected static $dol_admin_pass = 'admin';
61 private static $module_id = 500000; // TODO: autodetect?
62
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
90 protected function byHref($value)
91 {
92 $anchors = $this->elements($this->using('tag name')->value('a'));
93 $anchor = null;
94 foreach ($anchors as $test_anchor) {
95 if (\strstr($test_anchor->attribute('href'), $value)) {
96 $anchor = $test_anchor;
97 break;
98 }
99 }
100 return $anchor;
101 }
102
107 public static function setUpBeforeClass()
108 {
109 }
110
115 public function setUp()
116 {
117 $this->setSeleniumServerRequestsTimeout(3600);
118 $this->setBrowserUrl(self::$base_url);
119 }
120
125 protected function assertPreConditions()
126 {
127 }
128
134 private function authenticate()
135 {
136 try {
137 if ($this->byId('login')) {
138 $login = $this->byId('username');
139 $login->clear();
140 $login->value('admin');
141 $password = $this->byId('password');
142 $password->clear();
143 $password->value('admin');
144 $this->byId('login')->submit();
145 }
146 } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
147 // Login does not exist. Assume we are already authenticated
148 }
149 }
150
155 public function testEnableDeveloperMode()
156 {
157 $this->url('/admin/const.php');
158 $this->authenticate();
159 $main_features_level_path = '//input[@value="MAIN_FEATURES_LEVEL"]/following::input[@type="text"]';
160 $main_features_level = $this->byXPath($main_features_level_path);
161 $main_features_level->clear();
162 $main_features_level->value('2');
163 $this->byName('update')->click();
164 // Page reloaded, we need a new XPath
165 $main_features_level = $this->byXPath($main_features_level_path);
166 return $this->assertEquals('2', $main_features_level->value(), "MAIN_FEATURES_LEVEL value is 2");
167 }
168
175 public function testModuleEnabled()
176 {
177 $this->url('/admin/modules.php');
178 $this->authenticate();
179 $module_status_image_path = '//a[contains(@href, "'.self::$module_id.'")]/img';
180 $module_status_image = $this->byXPath($module_status_image_path);
181 if (\strstr($module_status_image->attribute('src'), 'switch_off.png')) {
182 // Enable the module
183 $this->byHref('modMyModule')->click();
184 } else {
185 // Disable the module
186 $this->byHref('modMyModule')->click();
187 // Re-enable the module
188 $this->byHref('modMyModule')->click();
189 }
190 // Page reloaded, we need a new Xpath
191 $module_status_image = $this->byXPath($module_status_image_path);
192 return $this->assertContains('switch_on.png', $module_status_image->attribute('src'), "Module enabled");
193 }
194
201 public function testConfigurationPage()
202 {
203 $this->url('/custom/mymodule/admin/setup.php');
204 $this->authenticate();
205 return $this->assertContains('mymodule/admin/setup.php', $this->url(), 'Configuration page');
206 }
207
214 public function testAboutPage()
215 {
216 $this->url('/custom/mymodule/admin/about.php');
217 $this->authenticate();
218 return $this->assertContains('mymodule/admin/about.php', $this->url(), 'About page');
219 }
220
228 {
229 $this->url('/custom/mymodule/admin/about.php');
230 $this->authenticate();
231 return $this->assertEquals(
232 'Dolibarr Module Template (aka My Module)',
233 $this->byTag('h1')->text(),
234 "Readme title"
235 );
236 }
237
244 public function testBoxDeclared()
245 {
246 $this->url('/admin/boxes.php');
247 $this->authenticate();
248 return $this->assertContains('mymodulewidget1', $this->source(), "Box enabled");
249 }
250
257 public function testTriggerDeclared()
258 {
259 $this->url('/admin/triggers.php');
260 $this->authenticate();
261 return $this->assertContains(
262 'interface_99_modMyModule_MyModuleTriggers.class.php',
263 $this->byTag('body')->text(),
264 "Trigger declared"
265 );
266 }
267
274 public function testTriggerEnabled()
275 {
276 $this->url('/admin/triggers.php');
277 $this->authenticate();
278 return $this->assertContains(
279 'tick.png',
280 $this->byXPath('//td[text()="interface_99_modMyModule_MyTrigger.class.php"]/following::img')->attribute('src'),
281 "Trigger enabled"
282 );
283 }
284
289 protected function assertPostConditions()
290 {
291 }
292
297 public function tearDown()
298 {
299 }
300
305 public static function tearDownAfterClass()
306 {
307 }
308}
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.