2022-11-06 06:41:05 +00:00
< ? php
2022-11-17 05:04:12 +00:00
use PHPUnit\Framework\TestCase ;
2022-11-06 06:41:05 +00:00
use PrivateBin\Data\Database ;
use PrivateBin\Data\Filesystem ;
2022-11-17 05:04:12 +00:00
class MigrateTest extends TestCase
2022-11-06 06:41:05 +00:00
{
protected $_model_1 ;
protected $_model_2 ;
protected $_path ;
protected $_path_instance_1 ;
protected $_path_instance_2 ;
2022-11-17 05:04:12 +00:00
public function setUp () : void
2022-11-06 06:41:05 +00:00
{
/* Setup Routine */
2022-11-06 06:43:19 +00:00
$this -> _path = sys_get_temp_dir () . DIRECTORY_SEPARATOR . 'privatebin_data' ;
2022-11-06 06:41:05 +00:00
$this -> _path_instance_1 = $this -> _path . DIRECTORY_SEPARATOR . 'instance_1' ;
$this -> _path_instance_2 = $this -> _path . DIRECTORY_SEPARATOR . 'instance_2' ;
if ( ! is_dir ( $this -> _path )) {
mkdir ( $this -> _path );
}
mkdir ( $this -> _path_instance_1 );
mkdir ( $this -> _path_instance_1 . DIRECTORY_SEPARATOR . 'cfg' );
mkdir ( $this -> _path_instance_2 );
mkdir ( $this -> _path_instance_2 . DIRECTORY_SEPARATOR . 'cfg' );
2022-11-06 06:43:19 +00:00
$options = parse_ini_file ( CONF_SAMPLE , true );
$options [ 'purge' ][ 'limit' ] = 0 ;
2022-11-06 06:41:05 +00:00
$options [ 'model_options' ][ 'dir' ] = $this -> _path_instance_1 . DIRECTORY_SEPARATOR . 'data' ;
2022-11-06 06:43:19 +00:00
$this -> _model_1 = new Filesystem ( $options [ 'model_options' ]);
2022-11-06 06:41:05 +00:00
Helper :: createIniFile ( $this -> _path_instance_1 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php' , $options );
$options [ 'model' ] = array (
'class' => 'Database' ,
);
$options [ 'model_options' ] = array (
'dsn' => 'sqlite:' . $this -> _path_instance_2 . DIRECTORY_SEPARATOR . 'test.sq3' ,
'usr' => null ,
'pwd' => null ,
'opt' => array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION ),
);
$this -> _model_2 = new Database ( $options [ 'model_options' ]);
Helper :: createIniFile ( $this -> _path_instance_2 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php' , $options );
}
2022-11-17 05:04:12 +00:00
public function tearDown () : void
2022-11-06 06:41:05 +00:00
{
/* Tear Down Routine */
Helper :: rmDir ( $this -> _path );
}
public function testMigrate ()
{
$this -> _model_1 -> delete ( Helper :: getPasteId ());
$this -> _model_2 -> delete ( Helper :: getPasteId ());
// storing paste & comment
$this -> _model_1 -> create ( Helper :: getPasteId (), Helper :: getPaste ());
$this -> _model_1 -> createComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId (), Helper :: getComment ());
// migrate files to database
2022-11-06 06:43:19 +00:00
$output = null ;
2022-11-06 06:41:05 +00:00
$exit_code = 255 ;
2022-11-06 06:43:19 +00:00
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 );
2022-11-06 06:41:05 +00:00
$this -> assertEquals ( 0 , $exit_code , 'migrate script exits 0' );
$this -> assertFalse ( $this -> _model_1 -> exists ( Helper :: getPasteId ()), 'paste removed after migrating it' );
$this -> assertFalse ( $this -> _model_1 -> existsComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId ()), 'comment removed after migrating it' );
$this -> assertTrue ( $this -> _model_2 -> exists ( Helper :: getPasteId ()), 'paste migrated' );
$this -> assertTrue ( $this -> _model_2 -> existsComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId ()), 'comment migrated' );
// migrate back to files
$exit_code = 255 ;
2022-11-06 06:43:19 +00:00
exec ( 'php ' . PATH . 'bin' . DIRECTORY_SEPARATOR . 'migrate ' . $this -> _path_instance_2 . DIRECTORY_SEPARATOR . 'cfg ' . $this -> _path_instance_1 . DIRECTORY_SEPARATOR . 'cfg' , $output , $exit_code );
2022-11-06 06:41:05 +00:00
$this -> assertEquals ( 0 , $exit_code , 'migrate script exits 0' );
$this -> assertTrue ( $this -> _model_1 -> exists ( Helper :: getPasteId ()), 'paste migrated back' );
$this -> assertTrue ( $this -> _model_1 -> existsComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId ()), 'comment migrated back' );
}
2022-11-06 06:43:19 +00:00
}