/test/classes/ThemeTest.php

http://github.com/phpmyadmin/phpmyadmin · PHP · 269 lines · 165 code · 34 blank · 70 comment · 0 complexity · bbc938462ec2f7d26920b06cb699bd85 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests;
  4. use PhpMyAdmin\Theme;
  5. use PhpMyAdmin\ThemeManager;
  6. use function filemtime;
  7. use const DIRECTORY_SEPARATOR;
  8. /**
  9. * @covers \PhpMyAdmin\Theme
  10. */
  11. class ThemeTest extends AbstractTestCase
  12. {
  13. /** @var Theme */
  14. protected $object;
  15. /** @var Theme backup for session theme */
  16. protected $backup;
  17. /**
  18. * Sets up the fixture, for example, opens a network connection.
  19. * This method is called before a test is executed.
  20. */
  21. protected function setUp(): void
  22. {
  23. global $theme;
  24. parent::setUp();
  25. parent::setTheme();
  26. $this->object = new Theme();
  27. $this->backup = $theme;
  28. $theme = $this->object;
  29. parent::setGlobalConfig();
  30. $GLOBALS['text_dir'] = 'ltr';
  31. $GLOBALS['server'] = '99';
  32. }
  33. /**
  34. * Tears down the fixture, for example, closes a network connection.
  35. * This method is called after a test is executed.
  36. */
  37. protected function tearDown(): void
  38. {
  39. global $theme;
  40. parent::tearDown();
  41. $theme = $this->backup;
  42. }
  43. /**
  44. * Test for Theme::loadInfo
  45. *
  46. * @group medium
  47. */
  48. public function testCheckImgPathNotExisted(): void
  49. {
  50. $this->object->setPath('path/to/nowhere');
  51. $this->assertFalse($this->object->loadInfo());
  52. }
  53. /**
  54. * Test for Theme::loadInfo
  55. */
  56. public function testCheckImgPathIncorrect(): void
  57. {
  58. $this->object->setPath(ROOT_PATH . 'test/classes/_data/incorrect_theme');
  59. $this->assertFalse(
  60. $this->object->loadInfo(),
  61. 'Theme name is not properly set'
  62. );
  63. }
  64. /**
  65. * Test for Theme::getName, getVersion
  66. */
  67. public function testCheckImgPathFull(): void
  68. {
  69. $this->object->setFsPath(ROOT_PATH . 'test/classes/_data/gen_version_info/');
  70. $this->assertTrue($this->object->loadInfo());
  71. $this->assertEquals('Test Theme', $this->object->getName());
  72. $this->assertEquals('5.1', $this->object->getVersion());
  73. }
  74. /**
  75. * Test for Theme::loadInfo
  76. */
  77. public function testLoadInfo(): void
  78. {
  79. $this->object->setFsPath(ROOT_PATH . 'themes/original/');
  80. $infofile = $this->object->getFsPath() . 'theme.json';
  81. $this->assertTrue($this->object->loadInfo());
  82. $this->assertEquals(
  83. filemtime($infofile),
  84. $this->object->mtimeInfo
  85. );
  86. $this->object->setPath(ROOT_PATH . 'themes/original');
  87. $this->object->mtimeInfo = (int) filemtime($infofile);
  88. $this->assertTrue($this->object->loadInfo());
  89. $this->assertEquals('Original', $this->object->getName());
  90. }
  91. /**
  92. * Test for Theme::load
  93. */
  94. public function testLoad(): void
  95. {
  96. $newTheme = Theme::load(
  97. ThemeManager::getThemesDir() . 'original',
  98. ThemeManager::getThemesFsDir() . 'original' . DIRECTORY_SEPARATOR,
  99. 'original'
  100. );
  101. $this->assertNotNull($newTheme);
  102. $this->assertInstanceOf(Theme::class, $newTheme);
  103. }
  104. /**
  105. * Test for Theme::load
  106. */
  107. public function testLoadNonExistent(): void
  108. {
  109. $this->assertNull(
  110. Theme::load(
  111. ThemeManager::getThemesDir() . 'nonexistent',
  112. ThemeManager::getThemesFsDir() . 'nonexistent' . DIRECTORY_SEPARATOR,
  113. 'nonexistent'
  114. )
  115. );
  116. }
  117. /**
  118. * Test fir Theme::checkImgPath
  119. */
  120. public function testCheckImgPathFallback(): void
  121. {
  122. $this->object->setPath('path/to/nowhere');
  123. $this->assertTrue($this->object->checkImgPath());
  124. }
  125. /**
  126. * Test for Theme::checkImgPath
  127. */
  128. public function testCheckImgPath(): void
  129. {
  130. $this->object->setPath(ROOT_PATH . 'themes/original');
  131. $this->assertTrue($this->object->checkImgPath());
  132. }
  133. /**
  134. * Test for Theme::getPath
  135. */
  136. public function testGetSetPath(): void
  137. {
  138. $this->assertEmpty($this->object->getPath());
  139. $this->object->setPath(ROOT_PATH . 'themes/original');
  140. $this->assertEquals(ROOT_PATH . 'themes/original', $this->object->getPath());
  141. }
  142. /**
  143. * Test for Theme::checkVersion
  144. *
  145. * @depends testLoadInfo
  146. */
  147. public function testGetSetCheckVersion(): void
  148. {
  149. $this->assertEquals(
  150. '0.0.0.0',
  151. $this->object->getVersion(),
  152. 'Version 0.0.0.0 by default'
  153. );
  154. $this->object->setVersion('1.2.3.4');
  155. $this->assertEquals('1.2.3.4', $this->object->getVersion());
  156. $this->assertFalse($this->object->checkVersion('0.0.1.1'));
  157. $this->assertTrue($this->object->checkVersion('2.0.1.1'));
  158. }
  159. /**
  160. * Test for Theme::getName
  161. */
  162. public function testGetSetName(): void
  163. {
  164. $this->assertEmpty($this->object->getName(), 'Name is empty by default');
  165. $this->object->setName('New Theme Name');
  166. $this->assertEquals('New Theme Name', $this->object->getName());
  167. }
  168. /**
  169. * Test for Theme::getId
  170. */
  171. public function testGetSetId(): void
  172. {
  173. $this->assertEmpty($this->object->getId(), 'ID is empty by default');
  174. $this->object->setId('NewID');
  175. $this->assertEquals('NewID', $this->object->getId());
  176. }
  177. /**
  178. * Test for Theme::getImgPath
  179. */
  180. public function testGetSetImgPath(): void
  181. {
  182. $this->assertEmpty(
  183. $this->object->getImgPath(),
  184. 'ImgPath is empty by default'
  185. );
  186. $this->object->setImgPath('/new/path');
  187. $this->assertEquals('/new/path', $this->object->getImgPath());
  188. }
  189. /**
  190. * Test for getImgPath
  191. *
  192. * @param string|null $file file name for image
  193. * @param string|null $fallback fallback image
  194. * @param string $output expected output
  195. *
  196. * @dataProvider providerForGetImgPath
  197. */
  198. public function testGetImgPath(?string $file, ?string $fallback, string $output): void
  199. {
  200. $this->assertEquals(
  201. $this->object->getImgPath($file, $fallback),
  202. $output
  203. );
  204. }
  205. /**
  206. * Provider for testGetImgPath
  207. *
  208. * @return array
  209. */
  210. public function providerForGetImgPath(): array
  211. {
  212. return [
  213. [
  214. null,
  215. null,
  216. '',
  217. ],
  218. [
  219. 'screen.png',
  220. null,
  221. './themes/pmahomme/img/screen.png',
  222. ],
  223. [
  224. 'arrow_ltr.png',
  225. null,
  226. './themes/pmahomme/img/arrow_ltr.png',
  227. ],
  228. [
  229. 'logo_right.png',
  230. 'pma_logo.png',
  231. './themes/pmahomme/img/pma_logo.png',
  232. ],
  233. ];
  234. }
  235. }