1
0

MigrateTest.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Data\Database;
  4. use PrivateBin\Data\Filesystem;
  5. class MigrateTest extends TestCase
  6. {
  7. protected $_model_1;
  8. protected $_model_2;
  9. protected $_path;
  10. protected $_path_instance_1;
  11. protected $_path_instance_2;
  12. public function setUp(): void
  13. {
  14. /* Setup Routine */
  15. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  16. $this->_path_instance_1 = $this->_path . DIRECTORY_SEPARATOR . 'instance_1';
  17. $this->_path_instance_2 = $this->_path . DIRECTORY_SEPARATOR . 'instance_2';
  18. if (!is_dir($this->_path)) {
  19. mkdir($this->_path);
  20. }
  21. mkdir($this->_path_instance_1);
  22. mkdir($this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg');
  23. mkdir($this->_path_instance_2);
  24. mkdir($this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg');
  25. $options = parse_ini_file(CONF_SAMPLE, true);
  26. $options['purge']['limit'] = 0;
  27. $options['model_options']['dir'] = $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'data';
  28. $this->_model_1 = new Filesystem($options['model_options']);
  29. Helper::createIniFile($this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php', $options);
  30. $options['model'] = array(
  31. 'class' => 'Database',
  32. );
  33. $options['model_options'] = array(
  34. 'dsn' => 'sqlite:' . $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'test.sq3',
  35. 'usr' => null,
  36. 'pwd' => null,
  37. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  38. );
  39. $this->_model_2 = new Database($options['model_options']);
  40. Helper::createIniFile($this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php', $options);
  41. }
  42. public function tearDown(): void
  43. {
  44. /* Tear Down Routine */
  45. Helper::rmDir($this->_path);
  46. }
  47. public function testMigrate()
  48. {
  49. $this->_model_1->delete(Helper::getPasteId());
  50. $this->_model_2->delete(Helper::getPasteId());
  51. // storing paste & comment
  52. $this->_model_1->create(Helper::getPasteId(), Helper::getPaste());
  53. $this->_model_1->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment());
  54. // migrate files to database
  55. $output = null;
  56. $exit_code = 255;
  57. exec('php ' . PATH . 'bin' . DIRECTORY_SEPARATOR . 'migrate --delete-after ' . $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg ' . $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg', $output, $exit_code);
  58. $this->assertEquals(0, $exit_code, 'migrate script exits 0');
  59. $this->assertFalse($this->_model_1->exists(Helper::getPasteId()), 'paste removed after migrating it');
  60. $this->assertFalse($this->_model_1->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment removed after migrating it');
  61. $this->assertTrue($this->_model_2->exists(Helper::getPasteId()), 'paste migrated');
  62. $this->assertTrue($this->_model_2->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment migrated');
  63. // migrate back to files
  64. $exit_code = 255;
  65. exec('php ' . PATH . 'bin' . DIRECTORY_SEPARATOR . 'migrate ' . $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg ' . $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg', $output, $exit_code);
  66. $this->assertEquals(0, $exit_code, 'migrate script exits 0');
  67. $this->assertTrue($this->_model_1->exists(Helper::getPasteId()), 'paste migrated back');
  68. $this->assertTrue($this->_model_1->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment migrated back');
  69. }
  70. }