TakePosFunctionalTest.php 7.2 KB

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