/**
  * @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/
  */
 public function aRecordWasLastEditedRelative($type, $id, $mod, $time)
 {
     $class = $this->convertTypeToClass($type);
     $fields = $this->prepareFixture($class, $id);
     $record = $this->fixtureFactory->createObject($class, $id, $fields);
     $date = date("Y-m-d H:i:s", strtotime($time));
     $table = \ClassInfo::baseDataClass(get_class($record));
     $field = $mod == 'created' ? 'Created' : 'LastEdited';
     \DB::query(sprintf('UPDATE "%s" SET "%s" = \'%s\' WHERE "ID" = \'%d\'', $table, $field, $date, $record->ID));
     // Support for Versioned extension, by checking for a "Live" stage
     if (\DB::getConn()->hasTable($table . '_Live')) {
         \DB::query(sprintf('UPDATE "%s_Live" SET "%s" = \'%s\' WHERE "ID" = \'%d\'', $table, $field, $date, $record->ID));
     }
 }
 public function createObject($name, $identifier, $data = null)
 {
     if (!$data) {
         $data = array();
     }
     // Copy identifier to some visible property unless its already defined.
     // Exclude files, since they generate their own named based on the file path.
     if (!$name != 'File' && !is_subclass_of($name, 'File')) {
         foreach (array('Name', 'Title') as $fieldName) {
             if (singleton($name)->hasField($fieldName) && !isset($data[$fieldName])) {
                 $data[$fieldName] = $identifier;
                 break;
             }
         }
     }
     return parent::createObject($name, $identifier, $data);
 }
Exemple #3
0
 /**
  * Persists the YAML data in a FixtureFactory,
  * which in turn saves them into the database.
  * Please use the passed in factory to access the fixtures afterwards.
  * 
  * @param  FixtureFactory $factory
  */
 public function writeInto(FixtureFactory $factory)
 {
     $parser = new Spyc();
     if (isset($this->fixtureString)) {
         $fixtureContent = $parser->load($this->fixtureString);
     } else {
         $fixtureContent = $parser->loadFile($this->fixtureFile);
     }
     foreach ($fixtureContent as $class => $items) {
         foreach ($items as $identifier => $data) {
             if (ClassInfo::exists($class)) {
                 $factory->createObject($class, $identifier, $data);
             } else {
                 $factory->createRaw($class, $identifier, $data);
             }
         }
     }
 }
 /**
  * Persists the YAML data in a FixtureFactory,
  * which in turn saves them into the database.
  * Please use the passed in factory to access the fixtures afterwards.
  *
  * @param  FixtureFactory $factory
  */
 public function writeInto(FixtureFactory $factory)
 {
     $parser = new Parser();
     if (isset($this->fixtureString)) {
         $fixtureContent = $parser->parse($this->fixtureString);
     } else {
         if (!file_exists($this->fixtureFile)) {
             return;
         }
         $contents = file_get_contents($this->fixtureFile);
         $fixtureContent = $parser->parse($contents);
         if (!$fixtureContent) {
             return;
         }
     }
     foreach ($fixtureContent as $class => $items) {
         foreach ($items as $identifier => $data) {
             if (ClassInfo::exists($class)) {
                 $factory->createObject($class, $identifier, $data);
             } else {
                 $factory->createRaw($class, $identifier, $data);
             }
         }
     }
 }
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // get schemas that need creating
     $schemas = $this->config()->get('default_schemas');
     require_once 'spyc/spyc.php';
     foreach ($schemas as $file) {
         if (file_exists(Director::baseFolder() . '/' . $file)) {
             $parser = new Spyc();
             $factory = new FixtureFactory();
             $fixtureContent = $parser->loadFile(Director::baseFolder() . '/' . $file);
             if (isset($fixtureContent['MetadataSchema'])) {
                 $toBuild = array();
                 // check if it exists or not, if so don't re-create it
                 foreach ($fixtureContent['MetadataSchema'] as $id => $desc) {
                     $name = isset($desc['Name']) ? $desc['Name'] : null;
                     if (!$name) {
                         throw new Exception("Cannot create metadata schema without a name");
                     }
                     $existing = MetadataSchema::get()->filter('Name', $name)->first();
                     if ($existing) {
                         $factory->setId('MetadataSchema', $id, $existing->ID);
                     } else {
                         $factory->createObject('MetadataSchema', $id, $desc);
                         DB::alteration_message('Metadata schema ' . $id . ' created', 'created');
                     }
                 }
                 // don't need this now
                 unset($fixtureContent['MetadataSchema']);
                 // go through and unset any existing fields
                 $toBuild = array();
                 foreach ($fixtureContent as $class => $items) {
                     foreach ($items as $identifier => $data) {
                         $nameField = isset($data['Name']) ? 'Name' : (isset($data['Key']) ? 'Key' : '');
                         if (!strlen($nameField)) {
                             throw new Exception("Metadata fields must have a Name or Key field defined");
                         }
                         if (!isset($data['Title'])) {
                             $data['Title'] = $data[$nameField];
                         }
                         $existing = $class::get()->filter($nameField, $data[$nameField])->first();
                         if ($existing) {
                             $factory->setId($class, $identifier, $existing->ID);
                         } else {
                             $factory->createObject($class, $identifier, $data);
                             DB::alteration_message('Metadata field ' . $data[$nameField] . ' created', 'created');
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Creates the object in the database as the original object will be wiped.
  *
  * @param string $class
  * @param string $identifier
  * @param array $data
  */
 public function createObject($class, $identifier, $data = null)
 {
     DB::alteration_message("Creating {$identifier} ({$class})", "created");
     if ($data) {
         foreach ($data as $k => $v) {
             if (!is_array($v) && preg_match('/^`(.)*`;$/', $v)) {
                 $str = substr($v, 1, -2);
                 $pv = null;
                 eval("\$pv = {$str};");
                 $data[$k] = $pv;
             }
         }
     }
     // for files copy the source dir if the image has a 'PopulateFileFrom'
     if (isset($data['PopulateFileFrom'])) {
         $folder = Folder::find_or_make(str_replace('assets/', '', dirname($data['Filename'])));
         @copy(BASE_PATH . '/' . $data['PopulateFileFrom'], BASE_PATH . '/' . $data['Filename']);
     }
     // if any merge labels are defined then we should create the object
     // from that
     $lookup = null;
     $mode = null;
     if (isset($data['PopulateMergeWhen'])) {
         $mode = 'PopulateMergeWhen';
         $lookup = DataList::create($class)->where($data['PopulateMergeWhen']);
         unset($data['PopulateMergeWhen']);
     } else {
         if (isset($data['PopulateMergeMatch'])) {
             $mode = 'PopulateMergeMatch';
             $filter = array();
             foreach ($data['PopulateMergeMatch'] as $field) {
                 $filter[$field] = $data[$field];
             }
             if (!$filter) {
                 throw new Exception('Not a valid PopulateMergeMatch filter');
             }
             $lookup = DataList::create($class)->filter($filter);
             unset($data['PopulateMergeMatch']);
         } else {
             if (isset($data['PopulateMergeAny'])) {
                 $mode = 'PopulateMergeAny';
                 $lookup = DataList::create($class);
                 unset($data['PopulateMergeAny']);
             }
         }
     }
     if ($lookup && $lookup->count() > 0) {
         $existing = $lookup->first();
         foreach ($lookup as $old) {
             if ($old->ID == $existing->ID) {
                 continue;
             }
             if ($old->hasExtension('Versioned')) {
                 foreach ($old->getVersionedStages() as $stage) {
                     $old->deleteFromStage($stage);
                 }
             }
             $old->delete();
         }
         $blueprint = new FixtureBlueprint($class);
         $obj = $blueprint->createObject($identifier, $data, $this->fixtures);
         $latest = $obj->toMap();
         unset($latest['ID']);
         $existing->update($latest);
         $existing->write();
         $obj->delete();
         $this->fixtures[$class][$identifier] = $existing->ID;
         $obj = $existing;
         $obj->flushCache();
     } else {
         $obj = parent::createObject($class, $identifier, $data);
     }
     if ($obj->hasExtension('Versioned')) {
         foreach ($obj->getVersionedStages() as $stage) {
             if ($stage !== $obj->getDefaultStage()) {
                 $obj->writeToStage($obj->getDefaultStage());
                 $obj->publish($obj->getDefaultStage(), $stage);
             }
         }
         $obj->flushCache();
     }
     return $obj;
 }
 public function testClearWithClass()
 {
     $factory = new FixtureFactory();
     $obj1 = $factory->createObject('FixtureFactoryTest_DataObject', 'object-one');
     $relation1 = $factory->createObject('FixtureFactoryTest_DataObjectRelation', 'relation-one');
     $factory->clear('FixtureFactoryTest_DataObject');
     $this->assertFalse($factory->getId('FixtureFactoryTest_DataObject', 'one'));
     $this->assertNull(FixtureFactoryTest_DataObject::get()->byId($obj1->ID));
     $this->assertEquals($relation1->ID, $factory->getId('FixtureFactoryTest_DataObjectRelation', 'relation-one'));
     $this->assertInstanceOf('FixtureFactoryTest_DataObjectRelation', FixtureFactoryTest_DataObjectRelation::get()->byId($relation1->ID));
 }