public function setUp()
 {
     parent::setUp();
     $schemafoo = array();
     $schemafoo['new']['new1']['label'] = 'pages';
     $schemafoo['new']['new1']['ismulti'] = 1;
     $schemafoo['new']['new1']['class'] = 'Page';
     $schemafoo['new']['new1']['isenabled'] = '1';
     $schemabar['new']['new2']['label'] = 'data';
     $schemabar['new']['new2']['ismulti'] = 0;
     $schemabar['new']['new2']['class'] = 'Text';
     $schemabar['new']['new2']['isenabled'] = '1';
     $builder_foo = new meta\SchemaBuilder('foo', $schemafoo);
     $builder_foo->build();
     $builder_bar = new meta\SchemaBuilder('bar', $schemabar);
     $builder_bar->build();
     $as = new mock\Assignments();
     $as->assignPageSchema('start', 'foo');
     $as->assignPageSchema('no:data', 'foo');
     $as->assignPageSchema('page1', 'bar');
     $as->assignPageSchema('page2', 'bar');
     $as->assignPageSchema('page2', 'bar');
     $this->saveData('start', 'foo', array('pages' => array('page1', 'page2')));
     $this->saveData('page1', 'bar', array('data' => 'data of page1'));
     $this->saveData('page2', 'bar', array('data' => 'data of page2'));
 }
 /**
  * Should carry out any processing required by the plugin.
  */
 public function handle()
 {
     global $INPUT;
     global $ID;
     global $config_cascade;
     $config_file_path = end($config_cascade['main']['local']);
     // form submit
     $table = Schema::cleanTableName($INPUT->str('table'));
     if ($table && $INPUT->bool('save') && checkSecurityToken()) {
         $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
         if (!$builder->build()) {
             msg('something went wrong while saving', -1);
         }
         touch($config_file_path);
     }
     // export
     if ($table && $INPUT->bool('export')) {
         $builder = new Schema($table);
         header('Content-Type: application/json');
         header("Content-Disposition: attachment; filename={$table}.struct.json");
         echo $builder->toJSON();
         exit;
     }
     // import
     if ($table && $INPUT->bool('import')) {
         if (isset($_FILES['schemafile']['tmp_name'])) {
             $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
             if (!$json) {
                 msg('Something went wrong with the upload', -1);
             } else {
                 $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
                 if (!$builder->build()) {
                     msg('something went wrong while saving', -1);
                 }
                 touch($config_file_path);
             }
         }
     }
     // delete
     if ($table && $INPUT->bool('delete')) {
         if ($table != $INPUT->str('confirm')) {
             msg($this->getLang('del_fail'), -1);
         } else {
             try {
                 $schema = new Schema($table);
                 $schema->delete();
                 msg($this->getLang('del_ok'), 1);
                 touch($config_file_path);
                 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
             } catch (StructException $e) {
                 msg(hsc($e->getMessage()), -1);
             }
         }
     }
 }
 public function test_export()
 {
     $sb = new meta\SchemaBuilder('schema1', array('new' => array('new1' => array('label' => 'first', 'class' => 'Text', 'sort' => 10, 'ismulti' => 0, 'isenabled' => 1), 'new2' => array('label' => 'second', 'class' => 'Text', 'sort' => 20, 'ismulti' => 1, 'isenabled' => 1), 'new3' => array('label' => 'third', 'class' => 'Text', 'sort' => 30, 'ismulti' => 0, 'isenabled' => 1), 'new4' => array('label' => 'fourth', 'class' => 'Text', 'sort' => 40, 'ismulti' => 0, 'isenabled' => 1))));
     $sb->build();
     $schema = new meta\Schema('schema1');
     $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true);
     $actual = json_decode($schema->toJSON(), true);
     // we don't expect this to match
     unset($expect['structversion']);
     unset($actual['structversion']);
     $this->assertEquals($expect, $actual);
 }
 public function test_build_update()
 {
     // arrange
     $initialdata = array();
     $initialdata['new']['new1']['sort'] = 70;
     $initialdata['new']['new1']['label'] = 'testcolumn';
     $initialdata['new']['new1']['ismulti'] = 0;
     $initialdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
     $initialdata['new']['new1']['class'] = 'Text';
     $initialdata['new']['new1']['isenabled'] = '1';
     $testname = 'testTable';
     $testname = Schema::cleanTableName($testname);
     $builder = new SchemaBuilder($testname, $initialdata);
     $result = $builder->build();
     $this->assertSame($result, '1', 'Prerequiste setup  in order to have basis which to change during act');
     $updatedata = array();
     $updatedata['id'] = "1";
     $updatedata['cols']['1']['sort'] = 65;
     $updatedata['cols']['1']['label'] = 'testColumn';
     $updatedata['cols']['1']['ismulti'] = 1;
     $updatedata['cols']['1']['config'] = '{"prefix": "pre", "postfix": "fix"}';
     $updatedata['cols']['1']['class'] = 'Text';
     $updatedata['cols']['1']['isenabled'] = '1';
     // act
     $builder = new SchemaBuilder($testname, $updatedata);
     $result = $builder->build();
     $res = $this->sqlite->query("SELECT * FROM types");
     $actual_types = $this->sqlite->res2arr($res);
     $this->sqlite->res_close($res);
     $expected_types = array(array('id' => "1", 'class' => 'Text', 'ismulti' => "0", 'label' => "testcolumn", 'config' => '{"prefix": "", "postfix": ""}'), array('id' => "2", 'class' => 'Text', 'ismulti' => "1", 'label' => "testColumn", 'config' => '{"prefix": "pre", "postfix": "fix"}'));
     // assert
     $this->assertSame($result, '2');
     $this->assertEquals($actual_types, $expected_types);
 }