TakePosFunctionalTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 SuperAdmin
  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. /**
  19. * \file test/functional/TakePosFunctionalTest.php
  20. * \ingroup takepos
  21. * \brief Example Selenium test.
  22. */
  23. namespace test\functional;
  24. use PHPUnit_Extensions_Selenium2TestCase_WebDriverException;
  25. /**
  26. * Class TakePosFunctionalTest
  27. *
  28. * Requires chromedriver for Google Chrome
  29. * Requires geckodriver for Mozilla Firefox
  30. *
  31. * @fixme Firefox (Geckodriver/Marionette) support
  32. * @todo Opera linux support
  33. * @todo Windows support (IE, Google Chrome, Mozilla Firefox, Safari)
  34. * @todo OSX support (Safari, Google Chrome, Mozilla Firefox)
  35. *
  36. * @package Testtakepos
  37. */
  38. class TakePosFunctionalTest extends \PHPUnit_Extensions_Selenium2TestCase
  39. {
  40. // TODO: move to a global configuration file?
  41. /** @var string Base URL of the webserver under test */
  42. protected static $base_url = 'http://dev.zenfusion.fr';
  43. /**
  44. * @var string Dolibarr admin username
  45. * @see authenticate
  46. */
  47. protected static $dol_admin_user = 'admin';
  48. /**
  49. * @var string Dolibarr admin password
  50. * @see authenticate
  51. */
  52. protected static $dol_admin_pass = 'admin';
  53. /** @var int Dolibarr module ID */
  54. private static $module_id = 500000; // TODO: autodetect?
  55. /** @var array Browsers to test with */
  56. public static $browsers = array(
  57. array(
  58. 'browser' => 'Google Chrome on Linux',
  59. 'browserName' => 'chrome',
  60. 'sessionStrategy' => 'shared',
  61. 'desiredCapabilities' => array()
  62. ),
  63. // Geckodriver does not keep the session at the moment?!
  64. // XPath selectors also don't seem to work
  65. //array(
  66. // 'browser' => 'Mozilla Firefox on Linux',
  67. // 'browserName' => 'firefox',
  68. // 'sessionStrategy' => 'shared',
  69. // 'desiredCapabilities' => array(
  70. // 'marionette' => true
  71. // )
  72. //)
  73. );
  74. /**
  75. * Helper function to select links by href
  76. *
  77. * @param string $value Href
  78. * @return mixed Helper string
  79. */
  80. protected function byHref($value)
  81. {
  82. $anchor = null;
  83. $anchors = $this->elements($this->using('tag name')->value('a'));
  84. foreach ($anchors as $anchor) {
  85. if (strstr($anchor->attribute('href'), $value)) {
  86. break;
  87. }
  88. }
  89. return $anchor;
  90. }
  91. /**
  92. * Global test setup
  93. *
  94. * @return void
  95. */
  96. public static function setUpBeforeClass(): void
  97. {
  98. }
  99. /**
  100. * Unit test setup
  101. *
  102. * @return void
  103. */
  104. public function setUp()
  105. {
  106. $this->setSeleniumServerRequestsTimeout(3600);
  107. $this->setBrowserUrl(self::$base_url);
  108. }
  109. /**
  110. * Verify pre conditions
  111. *
  112. * @return void
  113. */
  114. protected function assertPreConditions()
  115. {
  116. }
  117. /**
  118. * Handle Dolibarr authentication
  119. *
  120. * @return void
  121. */
  122. private function authenticate()
  123. {
  124. try {
  125. if ($this->byId('login')) {
  126. $login = $this->byId('username');
  127. $login->clear();
  128. $login->value('admin');
  129. $password = $this->byId('password');
  130. $password->clear();
  131. $password->value('admin');
  132. $this->byId('login')->submit();
  133. }
  134. } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
  135. // Login does not exist. Assume we are already authenticated
  136. }
  137. }
  138. /**
  139. * Test enabling developer mode
  140. *
  141. * @return void
  142. */
  143. public function testEnableDeveloperMode()
  144. {
  145. $this->url('/admin/const.php');
  146. $this->authenticate();
  147. $main_features_level_path='//input[@value="MAIN_FEATURES_LEVEL"]/following::input[@type="text"]';
  148. $main_features_level = $this->byXPath($main_features_level_path);
  149. $main_features_level->clear();
  150. $main_features_level->value('2');
  151. $this->byName('update')->click();
  152. // Page reloaded, we need a new XPath
  153. $main_features_level = $this->byXPath($main_features_level_path);
  154. $this->assertEquals('2', $main_features_level->value(), "MAIN_FEATURES_LEVEL value is 2");
  155. }
  156. /**
  157. * Test enabling the module
  158. *
  159. * @return void
  160. *
  161. * @depends testEnableDeveloperMode
  162. */
  163. public function testModuleEnabled()
  164. {
  165. $this->url('/admin/modules.php');
  166. $this->authenticate();
  167. $module_status_image_path='//a[contains(@href, "' . self::$module_id . '")]/img';
  168. $module_status_image = $this->byXPath($module_status_image_path);
  169. if (strstr($module_status_image->attribute('src'), 'switch_off.png')) {
  170. // Enable the module
  171. $this->byHref('modTakePos')->click();
  172. } else {
  173. // Disable the module
  174. $this->byHref('modTakePos')->click();
  175. // Reenable the module
  176. $this->byHref('modTakePos')->click();
  177. }
  178. // Page reloaded, we need a new Xpath
  179. $module_status_image = $this->byXPath($module_status_image_path);
  180. $this->assertContains('switch_on.png', $module_status_image->attribute('src'), "Module enabled");
  181. }
  182. /**
  183. * Test access to the configuration page
  184. *
  185. * @return void
  186. *
  187. * @depends testModuleEnabled
  188. */
  189. public function testConfigurationPage()
  190. {
  191. $this->url('/custom/takepos/admin/setup.php');
  192. $this->authenticate();
  193. $this->assertContains('takepos/admin/setup.php', $this->url(), 'Configuration page');
  194. }
  195. /**
  196. * Test access to the about page
  197. *
  198. * @return void
  199. *
  200. * @depends testConfigurationPage
  201. */
  202. public function testAboutPage()
  203. {
  204. $this->url('/custom/takepos/admin/about.php');
  205. $this->authenticate();
  206. $this->assertContains('takepos/admin/about.php', $this->url(), 'About page');
  207. }
  208. /**
  209. * Test about page is rendering Markdown
  210. *
  211. * @return void
  212. *
  213. * @depends testAboutPage
  214. */
  215. public function testAboutPageRendersMarkdownReadme()
  216. {
  217. $this->url('/custom/takepos/admin/about.php');
  218. $this->authenticate();
  219. $this->assertEquals(
  220. 'Dolibarr Module Template (aka My Module)',
  221. $this->byTag('h1')->text(),
  222. "Readme title"
  223. );
  224. }
  225. /**
  226. * Test box is properly declared
  227. *
  228. * @return void
  229. *
  230. * @depends testModuleEnabled
  231. */
  232. public function testBoxDeclared()
  233. {
  234. $this->url('/admin/boxes.php');
  235. $this->authenticate();
  236. $this->assertContains('takeposwidget1', $this->source(), "Box enabled");
  237. }
  238. /**
  239. * Test trigger is properly enabled
  240. *
  241. * @return void
  242. *
  243. * @depends testModuleEnabled
  244. */
  245. public function testTriggerDeclared()
  246. {
  247. $this->url('/admin/triggers.php');
  248. $this->authenticate();
  249. $this->assertContains(
  250. 'interface_99_modTakePos_TakePosTriggers.class.php',
  251. $this->byTag('body')->text(),
  252. "Trigger declared"
  253. );
  254. }
  255. /**
  256. * Test trigger is properly declared
  257. *
  258. * @return void
  259. *
  260. * @depends testTriggerDeclared
  261. */
  262. public function testTriggerEnabled()
  263. {
  264. $this->url('/admin/triggers.php');
  265. $this->authenticate();
  266. $this->assertContains(
  267. 'tick.png',
  268. $this->byXPath('//td[text()="interface_99_modTakePos_MyTrigger.class.php"]/following::img')->attribute('src'),
  269. "Trigger enabled"
  270. );
  271. }
  272. /**
  273. * Verify post conditions
  274. *
  275. * @return void
  276. */
  277. protected function assertPostConditions()
  278. {
  279. }
  280. /**
  281. * Unit test teardown
  282. *
  283. * @return void
  284. */
  285. public function tearDown()
  286. {
  287. }
  288. /**
  289. * Global test teardown
  290. *
  291. * @return void
  292. */
  293. public static function tearDownAfterClass(): void
  294. {
  295. }
  296. }