PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/integration/InstallerTest.php

https://bitbucket.org/chamilo/chamilo-install-dev/
PHP | 352 lines | 323 code | 24 blank | 5 comment | 3 complexity | 1abc9650f67253817d82ff27517eb019 MD5 | raw file
  1. <?php
  2. namespace install;
  3. use common\libraries\Filesystem;
  4. use common\libraries\Path;
  5. use common\libraries\Application;
  6. use common\libraries\CoreApplication;
  7. use common\libraries\WebApplication;
  8. use common\libraries\Connection;
  9. class InstallerTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /** @var install\Installer */
  12. private $installer;
  13. /** @var install\InstallerConfig */
  14. private $installer_config;
  15. /** @var string the config file containing all installation options */
  16. private $install_configuration_file;
  17. public $EXTRA_DIRECTORIES = array('archive', 'garbage', 'repository', 'temp', 'userpictures', 'scorm', 'logs',
  18. 'hotpotatoes');
  19. public $CORE_APPS = array('webservice', 'admin', 'help', 'reporting', 'tracking', 'repository', 'user', 'group',
  20. 'rights', 'home', 'menu', 'migration');
  21. function __construct()
  22. {
  23. parent :: __construct();
  24. $this->backupGlobals = false; // MDB needs this
  25. $this->install_configuration_file = __DIR__ . '/__files/install_config.php';
  26. }
  27. public function setUp()
  28. {
  29. $this->tune_error_reporting_for_mdb2();
  30. $this->installer_config = new InstallerConfig();
  31. $this->installer = new Installer($this->installer_config);
  32. $this->installer_config->load_config_file($this->install_configuration_file);
  33. }
  34. public function tearDown()
  35. {
  36. Filesystem :: remove(Path :: get_common_path() . 'configuration/configuration.php');
  37. foreach ($this->EXTRA_DIRECTORIES as $directory)
  38. {
  39. Filesystem :: remove(Path :: get(SYS_FILE_PATH) . $directory);
  40. }
  41. $this->reset_db();
  42. }
  43. private function reset_db()
  44. {
  45. $cx = $this->installer->get_db_connection();
  46. if (! is_null($cx))
  47. {
  48. $db_name = $cx->getDatabase();
  49. if ($cx->databaseExists($db_name))
  50. {
  51. $cx->loadModule('Manager');
  52. $tables = $cx->manager->listTables();
  53. foreach ($tables as $table)
  54. $cx->manager->dropTable($table);
  55. }
  56. }
  57. }
  58. private function tune_error_reporting_for_mdb2()
  59. {
  60. ini_set('error_reporting', E_ALL);
  61. }
  62. public function test_installation_should_create_config_file()
  63. {
  64. $this->installer_config->set_db_overwrite(false); // faster
  65. $this->assertFileNotExists(Path :: get_common_path() . 'configuration/configuration.php');
  66. $this->installer->perform_install();
  67. $this->assertFileExists(Path :: get_common_path() . 'configuration/configuration.php');
  68. }
  69. public function test_installation_should_create_extra_directories()
  70. {
  71. $this->installer_config->set_db_overwrite(false); // faster
  72. foreach ($this->EXTRA_DIRECTORIES as $directory)
  73. {
  74. $this->assertFileNotExists(Path :: get(SYS_FILE_PATH) . $directory);
  75. }
  76. $this->installer->perform_install();
  77. foreach ($this->EXTRA_DIRECTORIES as $directory)
  78. {
  79. $this->assertFileExists(Path :: get(SYS_FILE_PATH) . $directory);
  80. }
  81. }
  82. public function test_installation_should_register_all_content_objects()
  83. {
  84. //TODO : don't know how to automatic test without creating dependencies
  85. }
  86. public function test_installation_should_install_specified_extra_application()
  87. {
  88. //TODO : don't know how to automatic test without creating dependencies
  89. }
  90. public function test_installation_should_install_all_core_applications()
  91. {
  92. $this->installer->perform_install();
  93. foreach ($this->CORE_APPS as $application_name)
  94. {
  95. $this->assertTrue(CoreApplication :: exists($application_name));
  96. $application = CoreApplication :: factory($application_name);
  97. $this->assertTrue($application->is_active());
  98. }
  99. }
  100. public function test_installation_should_be_observable()
  101. {
  102. $observer1 = new test\DumbObserver();
  103. $observer2 = new test\DumbObserver();
  104. $this->installer->add_observer($observer1);
  105. $this->installer->add_observer($observer2);
  106. $this->installer->perform_install();
  107. $this->assertSame($observer1->getEvents(), $observer2->getEvents());
  108. $expected_array = array('before_install()', 'before_preprod()',
  109. 'preprod_config_file_written(InstallerTestResult : Success? : 1)',
  110. 'preprod_db_created(InstallerTestResult : Success? : 1)', 'after_preprod()',
  111. 'before_content_objects_install()', 'after_content_objects_install()',
  112. 'before_core_applications_install()', 'before_application_install(admin)',
  113. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(tracking)',
  114. 'after_application_install(InstallerTestResult : Success? : 1)',
  115. 'before_application_install(repository)',
  116. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(user)',
  117. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(group)',
  118. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(rights)',
  119. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(home)',
  120. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(menu)',
  121. 'after_application_install(InstallerTestResult : Success? : 1)',
  122. 'before_application_install(webservice)',
  123. 'after_application_install(InstallerTestResult : Success? : 1)', 'before_application_install(reporting)',
  124. 'after_application_install(InstallerTestResult : Success? : 1)',
  125. 'before_post_process_for_application(admin)',
  126. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  127. 'before_post_process_for_application(tracking)',
  128. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  129. 'before_post_process_for_application(repository)',
  130. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  131. 'before_post_process_for_application(user)',
  132. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  133. 'before_post_process_for_application(group)',
  134. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  135. 'before_post_process_for_application(rights)',
  136. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  137. 'before_post_process_for_application(home)',
  138. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  139. 'before_post_process_for_application(menu)',
  140. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  141. 'before_post_process_for_application(webservice)',
  142. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  143. 'before_post_process_for_application(reporting)',
  144. 'after_post_process_for_application(InstallerTestResult : Success? : 1)',
  145. 'after_core_applications_install()', 'before_optional_applications_install()',
  146. 'after_optional_applications_install()', 'before_filesystem_prepared()',
  147. 'after_filesystem_prepared(InstallerTestResult : Success? : 1)', 'after_install()');
  148. $this->assertSame($expected_array, $observer1->getEvents());
  149. }
  150. public function test_installation_should_fail_when_cannot_connect_the_db()
  151. {
  152. $this->installer_config->set_db_host('nonexistinghost');
  153. $this->installer_config->set_db_username('nobody');
  154. $this->installer_config->set_db_password('nothing');
  155. $this->setExpectedException('Exception');
  156. $this->installer->perform_install();
  157. }
  158. public function test_installation_should_create_db_if_nonexistent()
  159. {
  160. $cx = $this->installer->get_or_create_db_connection();
  161. $db_name = $this->installer_config->get_db_name();
  162. $dropResult = $cx->dropDatabase($db_name);
  163. $this->assertIsNotMDB2Error($dropResult);
  164. $this->installer->perform_install();
  165. $this->assertTrue($cx->databaseExists($db_name));
  166. }
  167. private function assertIsNotMDB2Error($mdb2Result)
  168. {
  169. if (\MDB2 :: isError($mdb2Result))
  170. {
  171. $this->fail("MDB2 shouldn't have returned an error : {$mdb2Result->getMessage()}");
  172. }
  173. }
  174. public function test_installation_should_overwrite_db_when_specified()
  175. {
  176. $this->add_dumb_table_in_db();
  177. $this->installer_config->set_db_overwrite(true);
  178. $this->installer->perform_install();
  179. $table_list = Connection :: get_instance()->get_connection()->listTables();
  180. $this->assertNotContains("dumb", $table_list);
  181. }
  182. private function add_dumb_table_in_db()
  183. {
  184. $definition = array('id' => array('type' => 'integer', 'unsigned' => 1, 'notnull' => 1, 'default' => 0),
  185. 'name' => array('type' => 'text', 'length' => 255), 'datetime' => array('type' => 'timestamp'));
  186. $mdb2->createTable('dumb', $definition);
  187. }
  188. public function test_installation_should_keep_db_when_specified()
  189. {
  190. $this->add_dumb_table_in_db();
  191. $this->installer_config->set_db_overwrite(false);
  192. $this->installer->perform_install();
  193. $table_list = Connection :: get_instance()->get_connection()->listTables();
  194. $this->assertContains("dumb", $table_list);
  195. }
  196. }
  197. namespace install\test;
  198. use install\InstallerObserver;
  199. use install\InstallerStepResult;
  200. class DumbObserver implements InstallerObserver
  201. {
  202. private $events = array();
  203. public function getEvents()
  204. {
  205. return $this->events;
  206. }
  207. public function after_application_install(InstallerStepResult $result)
  208. {
  209. $this->events[] = __FUNCTION__ . "({$result})";
  210. }
  211. public function after_filesystem_prepared(InstallerStepResult $result)
  212. {
  213. $this->events[] = __FUNCTION__ . "({$result})";
  214. }
  215. public function after_install()
  216. {
  217. $this->events[] = __FUNCTION__ . "()";
  218. }
  219. public function after_post_process()
  220. {
  221. $this->events[] = __FUNCTION__ . "()";
  222. }
  223. public function after_post_process_for_application(InstallerStepResult $result)
  224. {
  225. $this->events[] = __FUNCTION__ . "({$result})";
  226. }
  227. public function after_preprod()
  228. {
  229. $this->events[] = __FUNCTION__ . "()";
  230. }
  231. public function before_application_install($application)
  232. {
  233. $this->events[] = __FUNCTION__ . "({$application})";
  234. }
  235. public function before_filesystem_prepared()
  236. {
  237. $this->events[] = __FUNCTION__ . "()";
  238. }
  239. public function before_install()
  240. {
  241. $this->events[] = __FUNCTION__ . "()";
  242. }
  243. public function before_post_process()
  244. {
  245. $this->events[] = __FUNCTION__ . "()";
  246. }
  247. public function before_post_process_for_application($application)
  248. {
  249. $this->events[] = __FUNCTION__ . "({$application})";
  250. }
  251. public function before_preprod()
  252. {
  253. $this->events[] = __FUNCTION__ . "()";
  254. }
  255. public function preprod_config_file_written(InstallerStepResult $result)
  256. {
  257. $this->events[] = __FUNCTION__ . "({$result})";
  258. }
  259. public function preprod_db_created(InstallerStepResult $result)
  260. {
  261. $this->events[] = __FUNCTION__ . "({$result})";
  262. }
  263. public function after_content_object_install(InstallerStepResult $result)
  264. {
  265. $this->events[] = __FUNCTION__ . "({$result})";
  266. }
  267. public function after_content_objects_install()
  268. {
  269. $this->events[] = __FUNCTION__ . "()";
  270. }
  271. public function after_core_applications_install()
  272. {
  273. $this->events[] = __FUNCTION__ . "()";
  274. }
  275. public function after_optional_applications_install()
  276. {
  277. $this->events[] = __FUNCTION__ . "()";
  278. }
  279. public function before_content_object_install($content_object)
  280. {
  281. $this->events[] = __FUNCTION__ . "({$content_object})";
  282. }
  283. public function before_content_objects_install()
  284. {
  285. $this->events[] = __FUNCTION__ . "()";
  286. }
  287. public function before_core_applications_install()
  288. {
  289. $this->events[] = __FUNCTION__ . "()";
  290. }
  291. public function before_optional_applications_install()
  292. {
  293. $this->events[] = __FUNCTION__ . "()";
  294. }
  295. }