PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/TransformationsTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 368 lines | 291 code | 22 blank | 55 comment | 0 complexity | 0ab0c37d9570fe08898f8fbd53dd5c5d MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Transformations;
  6. use PhpMyAdmin\Version;
  7. /**
  8. * @covers \PhpMyAdmin\Transformations
  9. */
  10. class TransformationsTest extends AbstractTestCase
  11. {
  12. /** @var Transformations */
  13. private $transformations;
  14. /**
  15. * Set up global environment.
  16. */
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $GLOBALS['table'] = 'table';
  21. $GLOBALS['db'] = 'db';
  22. $GLOBALS['cfg'] = [
  23. 'ServerDefault' => 1,
  24. 'ActionLinksMode' => 'icons',
  25. ];
  26. $GLOBALS['server'] = 1;
  27. $GLOBALS['cfg']['Server']['pmadb'] = 'pmadb';
  28. $GLOBALS['cfg']['Server']['user'] = 'user';
  29. $GLOBALS['cfg']['Server']['bookmarktable'] = '';
  30. $GLOBALS['cfg']['Server']['relation'] = '';
  31. $GLOBALS['cfg']['Server']['table_info'] = '';
  32. $GLOBALS['cfg']['Server']['table_coords'] = '';
  33. $GLOBALS['cfg']['Server']['column_info'] = 'column_info';
  34. $GLOBALS['cfg']['DBG']['sql'] = false;
  35. // need to clear relation test cache
  36. unset($_SESSION['relation']);
  37. $this->transformations = new Transformations();
  38. }
  39. /**
  40. * Test for parsing options.
  41. *
  42. * @param string $input String to parse
  43. * @param array $expected Expected result
  44. *
  45. * @dataProvider getOptionsData
  46. */
  47. public function testGetOptions(string $input, array $expected): void
  48. {
  49. $this->assertEquals(
  50. $expected,
  51. $this->transformations->getOptions($input)
  52. );
  53. }
  54. /**
  55. * Data provided for parsing options
  56. */
  57. public function getOptionsData(): array
  58. {
  59. return [
  60. [
  61. 'option1 , option2 ',
  62. [
  63. 'option1 ',
  64. ' option2 ',
  65. ],
  66. ],
  67. [
  68. "'option1' ,' option2' ",
  69. [
  70. 'option1',
  71. ' option2',
  72. ],
  73. ],
  74. [
  75. "'2,3' ,' ,, option ,,' ",
  76. [
  77. '2,3',
  78. ' ,, option ,,',
  79. ],
  80. ],
  81. [
  82. "'',,",
  83. [
  84. '',
  85. '',
  86. '',
  87. ],
  88. ],
  89. [
  90. '',
  91. [],
  92. ],
  93. ];
  94. }
  95. /**
  96. * Test for getting available types.
  97. */
  98. public function testGetTypes(): void
  99. {
  100. $this->assertEquals(
  101. [
  102. 'mimetype' => [
  103. 'Application/Octetstream' => 'Application/Octetstream',
  104. 'Image/JPEG' => 'Image/JPEG',
  105. 'Image/PNG' => 'Image/PNG',
  106. 'Text/Plain' => 'Text/Plain',
  107. 'Text/Octetstream' => 'Text/Octetstream',
  108. ],
  109. 'transformation' => [
  110. 0 => 'Application/Octetstream: Download',
  111. 1 => 'Application/Octetstream: Hex',
  112. 2 => 'Image/JPEG: Inline',
  113. 3 => 'Image/JPEG: Link',
  114. 4 => 'Image/PNG: Inline',
  115. 5 => 'Text/Octetstream: Sql',
  116. 6 => 'Text/Plain: Binarytoip',
  117. 7 => 'Text/Plain: Bool2Text',
  118. 8 => 'Text/Plain: Dateformat',
  119. 9 => 'Text/Plain: External',
  120. 10 => 'Text/Plain: Formatted',
  121. 11 => 'Text/Plain: Imagelink',
  122. 12 => 'Text/Plain: Json',
  123. 13 => 'Text/Plain: Sql',
  124. 14 => 'Text/Plain: Xml',
  125. 15 => 'Text/Plain: Link',
  126. 16 => 'Text/Plain: Longtoipv4',
  127. 17 => 'Text/Plain: PreApPend',
  128. 18 => 'Text/Plain: Substring',
  129. ],
  130. 'transformation_file' => [
  131. 0 => 'Output/Application_Octetstream_Download.php',
  132. 1 => 'Output/Application_Octetstream_Hex.php',
  133. 2 => 'Output/Image_JPEG_Inline.php',
  134. 3 => 'Output/Image_JPEG_Link.php',
  135. 4 => 'Output/Image_PNG_Inline.php',
  136. 5 => 'Output/Text_Octetstream_Sql.php',
  137. 6 => 'Output/Text_Plain_Binarytoip.php',
  138. 7 => 'Output/Text_Plain_Bool2Text.php',
  139. 8 => 'Output/Text_Plain_Dateformat.php',
  140. 9 => 'Output/Text_Plain_External.php',
  141. 10 => 'Output/Text_Plain_Formatted.php',
  142. 11 => 'Output/Text_Plain_Imagelink.php',
  143. 12 => 'Output/Text_Plain_Json.php',
  144. 13 => 'Output/Text_Plain_Sql.php',
  145. 14 => 'Output/Text_Plain_Xml.php',
  146. 15 => 'Text_Plain_Link.php',
  147. 16 => 'Text_Plain_Longtoipv4.php',
  148. 17 => 'Text_Plain_PreApPend.php',
  149. 18 => 'Text_Plain_Substring.php',
  150. ],
  151. 'input_transformation' => [
  152. 'Image/JPEG: Upload',
  153. 'Text/Plain: FileUpload',
  154. 'Text/Plain: Iptobinary',
  155. 'Text/Plain: Iptolong',
  156. 'Text/Plain: JsonEditor',
  157. 'Text/Plain: RegexValidation',
  158. 'Text/Plain: SqlEditor',
  159. 'Text/Plain: XmlEditor',
  160. 'Text/Plain: Link',
  161. 'Text/Plain: Longtoipv4',
  162. 'Text/Plain: PreApPend',
  163. 'Text/Plain: Substring',
  164. ],
  165. 'input_transformation_file' => [
  166. 'Input/Image_JPEG_Upload.php',
  167. 'Input/Text_Plain_FileUpload.php',
  168. 'Input/Text_Plain_Iptobinary.php',
  169. 'Input/Text_Plain_Iptolong.php',
  170. 'Input/Text_Plain_JsonEditor.php',
  171. 'Input/Text_Plain_RegexValidation.php',
  172. 'Input/Text_Plain_SqlEditor.php',
  173. 'Input/Text_Plain_XmlEditor.php',
  174. 'Text_Plain_Link.php',
  175. 'Text_Plain_Longtoipv4.php',
  176. 'Text_Plain_PreApPend.php',
  177. 'Text_Plain_Substring.php',
  178. ],
  179. ],
  180. $this->transformations->getAvailableMimeTypes()
  181. );
  182. }
  183. /**
  184. * Tests getting mime types for table
  185. */
  186. public function testGetMime(): void
  187. {
  188. $_SESSION['relation'][$GLOBALS['server']]['version'] = Version::VERSION;
  189. $_SESSION['relation'][$GLOBALS['server']]['mimework'] = true;
  190. $_SESSION['relation'][$GLOBALS['server']]['db'] = 'pmadb';
  191. $_SESSION['relation'][$GLOBALS['server']]['column_info'] = 'column_info';
  192. $_SESSION['relation'][$GLOBALS['server']]['trackingwork'] = false;
  193. $this->assertEquals(
  194. [
  195. 'o' => [
  196. 'column_name' => 'o',
  197. 'mimetype' => 'Text/plain',
  198. 'transformation' => 'Sql',
  199. 'transformation_options' => '',
  200. 'input_transformation' => 'regex',
  201. 'input_transformation_options' => '/pma/i',
  202. ],
  203. 'col' => [
  204. 'column_name' => 'col',
  205. 'mimetype' => 'T',
  206. 'transformation' => 'O/P',
  207. 'transformation_options' => '',
  208. 'input_transformation' => 'i/p',
  209. 'input_transformation_options' => '',
  210. ],
  211. ],
  212. $this->transformations->getMime('pma_test', 'table1')
  213. );
  214. }
  215. /**
  216. * Test for clear
  217. */
  218. public function testClear(): void
  219. {
  220. // Mock dbi
  221. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  222. ->disableOriginalConstructor()
  223. ->getMock();
  224. $dbi->expects($this->any())
  225. ->method('tryQuery')
  226. ->will($this->returnValue(true));
  227. $GLOBALS['dbi'] = $dbi;
  228. // Case 1 : no configuration storage
  229. $actual = $this->transformations->clear('db');
  230. $this->assertFalse($actual);
  231. $_SESSION['relation'][$GLOBALS['server']]['version'] = Version::VERSION;
  232. $_SESSION['relation'][$GLOBALS['server']]['column_info'] = 'column_info';
  233. $_SESSION['relation'][$GLOBALS['server']]['db'] = 'pmadb';
  234. // Case 2 : database delete
  235. $actual = $this->transformations->clear('db');
  236. $this->assertTrue($actual);
  237. // Case 3 : table delete
  238. $actual = $this->transformations->clear('db', 'table');
  239. $this->assertTrue($actual);
  240. // Case 4 : column delete
  241. $actual = $this->transformations->clear('db', 'table', 'col');
  242. $this->assertTrue($actual);
  243. }
  244. /**
  245. * @param string $value value
  246. * @param string $expected expected result
  247. *
  248. * @dataProvider fixupData
  249. */
  250. public function testFixup(string $value, string $expected): void
  251. {
  252. $this->assertEquals(
  253. $expected,
  254. $this->transformations->fixUpMime($value)
  255. );
  256. }
  257. public function fixupData(): array
  258. {
  259. return [
  260. [
  261. 'text_plain_bool2text.php',
  262. 'Text_Plain_Bool2Text.php',
  263. ],
  264. [
  265. 'application_octetstream_download.php',
  266. 'Application_Octetstream_Download.php',
  267. ],
  268. [
  269. 'text_plain_json.php',
  270. 'Text_Plain_Json.php',
  271. ],
  272. [
  273. 'image_jpeg_link.php',
  274. 'Image_JPEG_Link.php',
  275. ],
  276. [
  277. 'text_plain_dateformat.php',
  278. 'Text_Plain_Dateformat.php',
  279. ],
  280. ];
  281. }
  282. /**
  283. * Test for getDescription
  284. *
  285. * @param string $file transformation file
  286. * @param string $expectedDescription expected description
  287. *
  288. * @dataProvider providerGetDescription
  289. */
  290. public function testGetDescription(string $file, string $expectedDescription): void
  291. {
  292. $this->assertEquals(
  293. $expectedDescription,
  294. $this->transformations->getDescription($file)
  295. );
  296. }
  297. public function providerGetDescription(): array
  298. {
  299. return [
  300. [
  301. '../../../../test',
  302. '',
  303. ],
  304. [
  305. 'Input/Text_Plain_SqlEditor',
  306. 'Syntax highlighted CodeMirror editor for SQL.',
  307. ],
  308. [
  309. 'Output/Text_Plain_Sql',
  310. 'Formats text as SQL query with syntax highlighting.',
  311. ],
  312. ];
  313. }
  314. /**
  315. * Test for getName
  316. *
  317. * @param string $file transformation file
  318. * @param string $expectedName expected name
  319. *
  320. * @dataProvider providerGetName
  321. */
  322. public function testGetName(string $file, string $expectedName): void
  323. {
  324. $this->assertEquals(
  325. $expectedName,
  326. $this->transformations->getName($file)
  327. );
  328. }
  329. public function providerGetName(): array
  330. {
  331. return [
  332. [
  333. '../../../../test',
  334. '',
  335. ],
  336. [
  337. 'Input/Text_Plain_SqlEditor',
  338. 'SQL',
  339. ],
  340. [
  341. 'Output/Text_Plain_Sql',
  342. 'SQL',
  343. ],
  344. ];
  345. }
  346. }