PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 398 lines | 231 code | 73 blank | 94 comment | 0 complexity | 474e42fc60f6d6c5df372e4c41793d1f MD5 | raw file
  1. <?php
  2. namespace Drupal\KernelTests\Core\Theme;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\Core\Extension\ExtensionNameLengthException;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * Tests installing and uninstalling of themes.
  8. *
  9. * @group Extension
  10. */
  11. class ThemeInstallerTest extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = array('system');
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function register(ContainerBuilder $container) {
  22. parent::register($container);
  23. // Some test methods involve ModuleHandler operations, which attempt to
  24. // rebuild and dump routes.
  25. $container
  26. ->register('router.dumper', 'Drupal\Core\Routing\NullMatcherDumper');
  27. }
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->installConfig(array('system'));
  31. }
  32. /**
  33. * Verifies that no themes are installed by default.
  34. */
  35. function testEmpty() {
  36. $this->assertFalse($this->extensionConfig()->get('theme'));
  37. $this->assertFalse(array_keys($this->themeHandler()->listInfo()));
  38. $this->assertFalse(array_keys(system_list('theme')));
  39. // Rebuilding available themes should always yield results though.
  40. $this->assertTrue($this->themeHandler()->rebuildThemeData()['stark'], 'ThemeHandler::rebuildThemeData() yields all available themes.');
  41. // theme_get_setting() should return global default theme settings.
  42. $this->assertIdentical(theme_get_setting('features.favicon'), TRUE);
  43. }
  44. /**
  45. * Tests installing a theme.
  46. */
  47. function testInstall() {
  48. $name = 'test_basetheme';
  49. $themes = $this->themeHandler()->listInfo();
  50. $this->assertFalse(isset($themes[$name]));
  51. $this->themeInstaller()->install(array($name));
  52. $this->assertIdentical($this->extensionConfig()->get("theme.$name"), 0);
  53. $themes = $this->themeHandler()->listInfo();
  54. $this->assertTrue(isset($themes[$name]));
  55. $this->assertEqual($themes[$name]->getName(), $name);
  56. $this->assertEqual(array_keys(system_list('theme')), array_keys($themes));
  57. // Verify that test_basetheme.settings is active.
  58. $this->assertIdentical(theme_get_setting('features.favicon', $name), FALSE);
  59. $this->assertEqual(theme_get_setting('base', $name), 'only');
  60. $this->assertEqual(theme_get_setting('override', $name), 'base');
  61. }
  62. /**
  63. * Tests installing a sub-theme.
  64. */
  65. function testInstallSubTheme() {
  66. $name = 'test_subtheme';
  67. $base_name = 'test_basetheme';
  68. $themes = $this->themeHandler()->listInfo();
  69. $this->assertFalse(array_keys($themes));
  70. $this->themeInstaller()->install(array($name));
  71. $themes = $this->themeHandler()->listInfo();
  72. $this->assertTrue(isset($themes[$name]));
  73. $this->assertTrue(isset($themes[$base_name]));
  74. $this->themeInstaller()->uninstall(array($name));
  75. $themes = $this->themeHandler()->listInfo();
  76. $this->assertFalse(isset($themes[$name]));
  77. $this->assertTrue(isset($themes[$base_name]));
  78. }
  79. /**
  80. * Tests installing a non-existing theme.
  81. */
  82. function testInstallNonExisting() {
  83. $name = 'non_existing_theme';
  84. $themes = $this->themeHandler()->listInfo();
  85. $this->assertFalse(array_keys($themes));
  86. try {
  87. $message = 'ThemeHandler::install() throws InvalidArgumentException upon installing a non-existing theme.';
  88. $this->themeInstaller()->install(array($name));
  89. $this->fail($message);
  90. }
  91. catch (\InvalidArgumentException $e) {
  92. $this->pass(get_class($e) . ': ' . $e->getMessage());
  93. }
  94. $themes = $this->themeHandler()->listInfo();
  95. $this->assertFalse(array_keys($themes));
  96. }
  97. /**
  98. * Tests installing a theme with a too long name.
  99. */
  100. function testInstallNameTooLong() {
  101. $name = 'test_theme_having_veery_long_name_which_is_too_long';
  102. try {
  103. $message = 'ThemeHandler::install() throws ExtensionNameLengthException upon installing a theme with a too long name.';
  104. $this->themeInstaller()->install(array($name));
  105. $this->fail($message);
  106. }
  107. catch (ExtensionNameLengthException $e) {
  108. $this->pass(get_class($e) . ': ' . $e->getMessage());
  109. }
  110. }
  111. /**
  112. * Tests uninstalling the default theme.
  113. */
  114. function testUninstallDefault() {
  115. $name = 'stark';
  116. $other_name = 'bartik';
  117. $this->themeInstaller()->install(array($name, $other_name));
  118. $this->themeHandler()->setDefault($name);
  119. $themes = $this->themeHandler()->listInfo();
  120. $this->assertTrue(isset($themes[$name]));
  121. $this->assertTrue(isset($themes[$other_name]));
  122. try {
  123. $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon disabling default theme.';
  124. $this->themeHandler()->uninstall(array($name));
  125. $this->fail($message);
  126. }
  127. catch (\InvalidArgumentException $e) {
  128. $this->pass(get_class($e) . ': ' . $e->getMessage());
  129. }
  130. $themes = $this->themeHandler()->listInfo();
  131. $this->assertTrue(isset($themes[$name]));
  132. $this->assertTrue(isset($themes[$other_name]));
  133. }
  134. /**
  135. * Tests uninstalling the admin theme.
  136. */
  137. function testUninstallAdmin() {
  138. $name = 'stark';
  139. $other_name = 'bartik';
  140. $this->themeInstaller()->install(array($name, $other_name));
  141. $this->config('system.theme')->set('admin', $name)->save();
  142. $themes = $this->themeHandler()->listInfo();
  143. $this->assertTrue(isset($themes[$name]));
  144. $this->assertTrue(isset($themes[$other_name]));
  145. try {
  146. $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon disabling admin theme.';
  147. $this->themeHandler()->uninstall(array($name));
  148. $this->fail($message);
  149. }
  150. catch (\InvalidArgumentException $e) {
  151. $this->pass(get_class($e) . ': ' . $e->getMessage());
  152. }
  153. $themes = $this->themeHandler()->listInfo();
  154. $this->assertTrue(isset($themes[$name]));
  155. $this->assertTrue(isset($themes[$other_name]));
  156. }
  157. /**
  158. * Tests uninstalling a sub-theme.
  159. */
  160. function testUninstallSubTheme() {
  161. $name = 'test_subtheme';
  162. $base_name = 'test_basetheme';
  163. $this->themeInstaller()->install(array($name));
  164. $this->themeInstaller()->uninstall(array($name));
  165. $themes = $this->themeHandler()->listInfo();
  166. $this->assertFalse(isset($themes[$name]));
  167. $this->assertTrue(isset($themes[$base_name]));
  168. }
  169. /**
  170. * Tests uninstalling a base theme before its sub-theme.
  171. */
  172. function testUninstallBaseBeforeSubTheme() {
  173. $name = 'test_basetheme';
  174. $sub_name = 'test_subtheme';
  175. $this->themeInstaller()->install(array($sub_name));
  176. try {
  177. $message = 'ThemeHandler::install() throws InvalidArgumentException upon uninstalling base theme before sub theme.';
  178. $this->themeInstaller()->uninstall(array($name));
  179. $this->fail($message);
  180. }
  181. catch (\InvalidArgumentException $e) {
  182. $this->pass(get_class($e) . ': ' . $e->getMessage());
  183. }
  184. $themes = $this->themeHandler()->listInfo();
  185. $this->assertTrue(isset($themes[$name]));
  186. $this->assertTrue(isset($themes[$sub_name]));
  187. // Verify that uninstalling both at the same time works.
  188. $this->themeInstaller()->uninstall(array($name, $sub_name));
  189. $themes = $this->themeHandler()->listInfo();
  190. $this->assertFalse(isset($themes[$name]));
  191. $this->assertFalse(isset($themes[$sub_name]));
  192. }
  193. /**
  194. * Tests uninstalling a non-existing theme.
  195. */
  196. function testUninstallNonExisting() {
  197. $name = 'non_existing_theme';
  198. $themes = $this->themeHandler()->listInfo();
  199. $this->assertFalse(array_keys($themes));
  200. try {
  201. $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a non-existing theme.';
  202. $this->themeInstaller()->uninstall(array($name));
  203. $this->fail($message);
  204. }
  205. catch (\InvalidArgumentException $e) {
  206. $this->pass(get_class($e) . ': ' . $e->getMessage());
  207. }
  208. $themes = $this->themeHandler()->listInfo();
  209. $this->assertFalse(array_keys($themes));
  210. }
  211. /**
  212. * Tests uninstalling a theme.
  213. */
  214. function testUninstall() {
  215. $name = 'test_basetheme';
  216. $this->themeInstaller()->install(array($name));
  217. $this->assertTrue($this->config("$name.settings")->get());
  218. $this->themeInstaller()->uninstall(array($name));
  219. $this->assertFalse(array_keys($this->themeHandler()->listInfo()));
  220. $this->assertFalse(array_keys(system_list('theme')));
  221. $this->assertFalse($this->config("$name.settings")->get());
  222. // Ensure that the uninstalled theme can be installed again.
  223. $this->themeInstaller()->install(array($name));
  224. $themes = $this->themeHandler()->listInfo();
  225. $this->assertTrue(isset($themes[$name]));
  226. $this->assertEqual($themes[$name]->getName(), $name);
  227. $this->assertEqual(array_keys(system_list('theme')), array_keys($themes));
  228. $this->assertTrue($this->config("$name.settings")->get());
  229. }
  230. /**
  231. * Tests uninstalling a theme that is not installed.
  232. */
  233. function testUninstallNotInstalled() {
  234. $name = 'test_basetheme';
  235. try {
  236. $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a theme that is not installed.';
  237. $this->themeInstaller()->uninstall(array($name));
  238. $this->fail($message);
  239. }
  240. catch (\InvalidArgumentException $e) {
  241. $this->pass(get_class($e) . ': ' . $e->getMessage());
  242. }
  243. }
  244. /**
  245. * Tests that theme info can be altered by a module.
  246. *
  247. * @see module_test_system_info_alter()
  248. */
  249. function testThemeInfoAlter() {
  250. $name = 'seven';
  251. $this->container->get('state')->set('module_test.hook_system_info_alter', TRUE);
  252. $this->themeInstaller()->install(array($name));
  253. $themes = $this->themeHandler()->listInfo();
  254. $this->assertFalse(isset($themes[$name]->info['regions']['test_region']));
  255. // Rebuild module data so we know where module_test is located.
  256. // @todo Remove as part of https://www.drupal.org/node/2186491
  257. system_rebuild_module_data();
  258. $this->moduleInstaller()->install(array('module_test'), FALSE);
  259. $this->assertTrue($this->moduleHandler()->moduleExists('module_test'));
  260. $themes = $this->themeHandler()->listInfo();
  261. $this->assertTrue(isset($themes[$name]->info['regions']['test_region']));
  262. // Legacy assertions.
  263. // @todo Remove once theme initialization/info has been modernized.
  264. // @see https://www.drupal.org/node/2228093
  265. $info = system_get_info('theme', $name);
  266. $this->assertTrue(isset($info['regions']['test_region']));
  267. $regions = system_region_list($name);
  268. $this->assertTrue(isset($regions['test_region']));
  269. $system_list = system_list('theme');
  270. $this->assertTrue(isset($system_list[$name]->info['regions']['test_region']));
  271. $this->moduleInstaller()->uninstall(array('module_test'));
  272. $this->assertFalse($this->moduleHandler()->moduleExists('module_test'));
  273. $themes = $this->themeHandler()->listInfo();
  274. $this->assertFalse(isset($themes[$name]->info['regions']['test_region']));
  275. // Legacy assertions.
  276. // @todo Remove once theme initialization/info has been modernized.
  277. // @see https://www.drupal.org/node/2228093
  278. $info = system_get_info('theme', $name);
  279. $this->assertFalse(isset($info['regions']['test_region']));
  280. $regions = system_region_list($name);
  281. $this->assertFalse(isset($regions['test_region']));
  282. $system_list = system_list('theme');
  283. $this->assertFalse(isset($system_list[$name]->info['regions']['test_region']));
  284. }
  285. /**
  286. * Returns the theme handler service.
  287. *
  288. * @return \Drupal\Core\Extension\ThemeHandlerInterface
  289. */
  290. protected function themeHandler() {
  291. return $this->container->get('theme_handler');
  292. }
  293. /**
  294. * Returns the theme installer service.
  295. *
  296. * @return \Drupal\Core\Extension\ThemeInstallerInterface
  297. */
  298. protected function themeInstaller() {
  299. return $this->container->get('theme_installer');
  300. }
  301. /**
  302. * Returns the system.theme config object.
  303. *
  304. * @return \Drupal\Core\Config\Config
  305. */
  306. protected function extensionConfig() {
  307. return $this->config('core.extension');
  308. }
  309. /**
  310. * Returns the ModuleHandler.
  311. *
  312. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  313. */
  314. protected function moduleHandler() {
  315. return $this->container->get('module_handler');
  316. }
  317. /**
  318. * Returns the ModuleInstaller.
  319. *
  320. * @return \Drupal\Core\Extension\ModuleInstallerInterface
  321. */
  322. protected function moduleInstaller() {
  323. return $this->container->get('module_installer');
  324. }
  325. }