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

/test/classes/Plugins/Import/ImportShpTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 215 lines | 147 code | 23 blank | 45 comment | 0 complexity | 89125f2f8f5dc9f2377e4195158ee7de MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests\Plugins\Import;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\File;
  6. use PhpMyAdmin\Plugins\Import\ImportShp;
  7. use PhpMyAdmin\Tests\AbstractTestCase;
  8. use function __;
  9. use function extension_loaded;
  10. /**
  11. * @covers \PhpMyAdmin\Plugins\Import\ImportShp
  12. * @requires extension zip
  13. */
  14. class ImportShpTest extends AbstractTestCase
  15. {
  16. /** @var ImportShp */
  17. protected $object;
  18. /**
  19. * Sets up the fixture, for example, opens a network connection.
  20. * This method is called before a test is executed.
  21. *
  22. * @access protected
  23. */
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $GLOBALS['server'] = 0;
  28. //setting
  29. $GLOBALS['plugin_param'] = 'table';
  30. $GLOBALS['finished'] = false;
  31. $GLOBALS['read_limit'] = 100000000;
  32. $GLOBALS['offset'] = 0;
  33. $GLOBALS['cfg']['Server']['DisableIS'] = false;
  34. //Mock DBI
  35. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $GLOBALS['dbi'] = $dbi;
  39. $this->object = new ImportShp();
  40. $GLOBALS['compression'] = 'application/zip';
  41. $GLOBALS['read_multiply'] = 10;
  42. $GLOBALS['import_type'] = 'ods';
  43. unset($GLOBALS['db'], $GLOBALS['table']);
  44. }
  45. /**
  46. * Executes import of given file
  47. *
  48. * @param string $filename Name of test file
  49. */
  50. protected function runImport(string $filename): void
  51. {
  52. $GLOBALS['import_file'] = $filename;
  53. $importHandle = new File($filename);
  54. $importHandle->setDecompressContent(true);
  55. $importHandle->open();
  56. $GLOBALS['message'] = '';
  57. $GLOBALS['error'] = false;
  58. $this->object->doImport($importHandle);
  59. $this->assertEquals('', $GLOBALS['message']);
  60. $this->assertFalse($GLOBALS['error']);
  61. }
  62. /**
  63. * Tears down the fixture, for example, closes a network connection.
  64. * This method is called after a test is executed.
  65. *
  66. * @access protected
  67. */
  68. protected function tearDown(): void
  69. {
  70. parent::tearDown();
  71. unset($this->object);
  72. }
  73. /**
  74. * Test for getProperties
  75. *
  76. * @group medium
  77. */
  78. public function testGetProperties(): void
  79. {
  80. $properties = $this->object->getProperties();
  81. $this->assertEquals(
  82. __('ESRI Shape File'),
  83. $properties->getText()
  84. );
  85. $this->assertEquals(
  86. 'shp',
  87. $properties->getExtension()
  88. );
  89. $this->assertNull($properties->getOptions());
  90. $this->assertEquals(
  91. __('Options'),
  92. $properties->getOptionsText()
  93. );
  94. }
  95. /**
  96. * Test for doImport with complex data
  97. *
  98. * @group medium
  99. * @group 32bit-incompatible
  100. */
  101. public function testImportOsm(): void
  102. {
  103. //$sql_query_disabled will show the import SQL detail
  104. //$import_notice will show the import detail result
  105. global $import_notice, $sql_query, $sql_query_disabled;
  106. $sql_query_disabled = false;
  107. //Test function called
  108. $this->runImport('test/test_data/dresden_osm.shp.zip');
  109. $this->assertMessages($import_notice);
  110. $endsWith = "13.737122 51.0542065)))'))";
  111. if (extension_loaded('dbase')) {
  112. $endsWith = "13.737122 51.0542065)))'),";
  113. }
  114. $this->assertStringContainsString(
  115. "(GeomFromText('MULTIPOLYGON((("
  116. . '13.737122 51.0542065,'
  117. . '13.7373039 51.0541298,'
  118. . '13.7372661 51.0540944,'
  119. . '13.7370842 51.0541711,'
  120. . $endsWith,
  121. $sql_query
  122. );
  123. }
  124. /**
  125. * Test for doImport
  126. *
  127. * @group medium
  128. * @group 32bit-incompatible
  129. */
  130. public function testDoImport(): void
  131. {
  132. //$sql_query_disabled will show the import SQL detail
  133. //$import_notice will show the import detail result
  134. global $import_notice, $sql_query, $sql_query_disabled;
  135. $sql_query_disabled = false;
  136. //Test function called
  137. $this->runImport('test/test_data/timezone.shp.zip');
  138. // asset that all sql are executed
  139. $this->assertStringContainsString(
  140. 'CREATE DATABASE IF NOT EXISTS `SHP_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci',
  141. $sql_query
  142. );
  143. // dbase extension will generate different sql statement
  144. if (extension_loaded('dbase')) {
  145. $this->assertStringContainsString(
  146. 'CREATE TABLE IF NOT EXISTS `SHP_DB`.`TBL_NAME` '
  147. . '(`SPATIAL` geometry, `ID` int(2), `AUTHORITY` varchar(25), `NAME` varchar(42)) '
  148. . 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;',
  149. $sql_query
  150. );
  151. $this->assertStringContainsString(
  152. 'INSERT INTO `SHP_DB`.`TBL_NAME` (`SPATIAL`, `ID`, `AUTHORITY`, `NAME`) VALUES',
  153. $sql_query
  154. );
  155. } else {
  156. $this->assertStringContainsString(
  157. 'CREATE TABLE IF NOT EXISTS `SHP_DB`.`TBL_NAME` (`SPATIAL` geometry)',
  158. $sql_query
  159. );
  160. $this->assertStringContainsString('INSERT INTO `SHP_DB`.`TBL_NAME` (`SPATIAL`) VALUES', $sql_query);
  161. }
  162. $this->assertStringContainsString("GeomFromText('POINT(1294523.1759236", $sql_query);
  163. //asset that all databases and tables are imported
  164. $this->assertMessages($import_notice);
  165. }
  166. /**
  167. * Validates import messages
  168. *
  169. * @param string $import_notice Messages to check
  170. */
  171. protected function assertMessages(string $import_notice): void
  172. {
  173. $this->assertStringContainsString(
  174. 'The following structures have either been created or altered.',
  175. $import_notice
  176. );
  177. $this->assertStringContainsString('Go to database: `SHP_DB`', $import_notice);
  178. $this->assertStringContainsString('Edit settings for `SHP_DB`', $import_notice);
  179. $this->assertStringContainsString('Go to table: `TBL_NAME`', $import_notice);
  180. $this->assertStringContainsString('Edit settings for `TBL_NAME`', $import_notice);
  181. //asset that the import process is finished
  182. $this->assertTrue($GLOBALS['finished']);
  183. }
  184. }