MigrateForceOverwriteTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Data\Filesystem;
  4. class MigrateForceOverwriteTest extends TestCase
  5. {
  6. private $_destination;
  7. private $_path;
  8. private $_source;
  9. public function setUp(): void
  10. {
  11. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_migrate_force';
  12. Helper::rmDir($this->_path);
  13. mkdir($this->_path);
  14. mkdir($this->_path . DIRECTORY_SEPARATOR . 'source_cfg');
  15. mkdir($this->_path . DIRECTORY_SEPARATOR . 'destination_cfg');
  16. $options = parse_ini_file(CONF_SAMPLE, true);
  17. $options['model_options']['dir'] = $this->_path . DIRECTORY_SEPARATOR . 'source';
  18. $this->_source = new Filesystem($options['model_options']);
  19. Helper::createIniFile(
  20. $this->_path . DIRECTORY_SEPARATOR . 'source_cfg' . DIRECTORY_SEPARATOR . 'conf.php',
  21. $options
  22. );
  23. $options['model_options']['dir'] = $this->_path . DIRECTORY_SEPARATOR . 'destination';
  24. $this->_destination = new Filesystem($options['model_options']);
  25. Helper::createIniFile(
  26. $this->_path . DIRECTORY_SEPARATOR . 'destination_cfg' . DIRECTORY_SEPARATOR . 'conf.php',
  27. $options
  28. );
  29. }
  30. public function tearDown(): void
  31. {
  32. Helper::rmDir($this->_path);
  33. }
  34. public function testForceReplacesPasteAndDiscussion()
  35. {
  36. $sourcePaste = Helper::getPaste();
  37. $sourcePaste['ct'] = 'source paste';
  38. $this->_source->create(Helper::getPasteId(), $sourcePaste);
  39. $sourceComment = Helper::getComment();
  40. $sourceComment['ct'] = 'source comment';
  41. $this->_source->createComment(
  42. Helper::getPasteId(),
  43. Helper::getPasteId(),
  44. Helper::getCommentId(),
  45. $sourceComment
  46. );
  47. $destinationPaste = Helper::getPaste();
  48. $destinationPaste['ct'] = 'old paste';
  49. $this->_destination->create(Helper::getPasteId(), $destinationPaste);
  50. $destinationComment = Helper::getComment();
  51. $destinationComment['ct'] = 'old comment';
  52. $this->_destination->createComment(
  53. Helper::getPasteId(),
  54. Helper::getPasteId(),
  55. Helper::getCommentId(),
  56. $destinationComment
  57. );
  58. $command = escapeshellarg(PHP_BINARY) . ' ' .
  59. escapeshellarg(realpath(PATH . 'bin' . DIRECTORY_SEPARATOR . 'migrate')) .
  60. ' -f --delete-after ' .
  61. escapeshellarg($this->_path . DIRECTORY_SEPARATOR . 'source_cfg') . ' ' .
  62. escapeshellarg($this->_path . DIRECTORY_SEPARATOR . 'destination_cfg') .
  63. ' 2>&1';
  64. exec($command, $output, $exitCode);
  65. $this->assertSame(0, $exitCode, implode(PHP_EOL, $output));
  66. $this->assertFalse($this->_source->exists(Helper::getPasteId()));
  67. $this->assertSame(
  68. 'source paste',
  69. $this->_destination->read(Helper::getPasteId())['ct']
  70. );
  71. $comments = array_values($this->_destination->readComments(Helper::getPasteId()));
  72. $this->assertCount(1, $comments);
  73. $this->assertSame('source comment', $comments[0]['ct']);
  74. }
  75. }