2008-08-11 01:17:51 +02:00
|
|
|
<?php
|
2008-08-17 03:08:10 +02:00
|
|
|
|
2009-11-21 03:29:59 +01:00
|
|
|
require_once 'thirdparty/spyc/spyc.php';
|
2008-08-17 03:08:10 +02:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* Uses the Spyc library to parse a YAML document (see http://yaml.org).
|
|
|
|
* YAML is a simple markup languages that uses tabs and colons instead of the more verbose XML tags,
|
|
|
|
* and because of this much better for developers creating files by hand.
|
|
|
|
*
|
|
|
|
* The contents of the YAML file are broken into three levels:
|
|
|
|
* - Top level: class names - Page and ErrorPage. This is the name of the dataobject class that should be created.
|
|
|
|
* The fact that ErrorPage is actually a subclass is irrelevant to the system populating the database.
|
|
|
|
* Each identifier you specify delimits a new database record.
|
|
|
|
* This means that every record needs to have an identifier, whether you use it or not.
|
|
|
|
* - Third level: fields - each field for the record is listed as a 3rd level entry.
|
2012-01-10 04:15:03 +01:00
|
|
|
* In most cases, the field's raw content is provided.
|
2008-08-11 01:17:51 +02:00
|
|
|
* However, if you want to define a relationship, you can do so using "=>"
|
|
|
|
*
|
|
|
|
* There are a couple of lines like this:
|
2010-04-23 02:11:41 +02:00
|
|
|
* <code>
|
|
|
|
* Parent: =>Page.about
|
|
|
|
* </code>
|
2012-01-10 04:15:03 +01:00
|
|
|
* This will tell the system to set the ParentID database field to the ID of the Page object with the identifier 'about'.
|
2008-08-11 01:17:51 +02:00
|
|
|
* This can be used on any has-one or many-many relationship.
|
|
|
|
* Note that we use the name of the relationship (Parent), and not the name of the database field (ParentID)
|
|
|
|
*
|
|
|
|
* On many-many relationships, you should specify a comma separated list of values.
|
2010-04-23 02:11:41 +02:00
|
|
|
* <code>
|
|
|
|
* MyRelation: =>Class.inst1,=>Class.inst2,=>Class.inst3
|
|
|
|
* </code>
|
|
|
|
*
|
2008-08-11 01:17:51 +02:00
|
|
|
* An crucial thing to note is that the YAML file specifies DataObjects, not database records.
|
|
|
|
* The database is populated by instantiating DataObject objects, setting the fields listed, and calling write().
|
|
|
|
* This means that any onBeforeWrite() or default value logic will be executed as part of the test.
|
|
|
|
* This forms the basis of our testURLGeneration() test above.
|
|
|
|
*
|
|
|
|
* For example, the URLSegment value of Page.staffduplicate is the same as the URLSegment value of Page.staff.
|
|
|
|
* When the fixture is set up, the URLSegment value of Page.staffduplicate will actually be my-staff-2.
|
|
|
|
*
|
|
|
|
* Finally, be aware that requireDefaultRecords() is not called by the database populator -
|
|
|
|
* so you will need to specify standard pages such as 404 and home in your YAML file.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* Page:
|
|
|
|
* home:
|
|
|
|
* Title: Home
|
|
|
|
* about:
|
|
|
|
* Title: About Us
|
|
|
|
* staff:
|
|
|
|
* Title: Staff
|
|
|
|
* URLSegment: my-staff
|
|
|
|
* Parent: =>Page.about
|
|
|
|
* staffduplicate:
|
|
|
|
* Title: Staff
|
|
|
|
* URLSegment: my-staff
|
|
|
|
* Parent: =>Page.about
|
|
|
|
* products:
|
|
|
|
* Title: Products
|
|
|
|
* ErrorPage:
|
|
|
|
* 404:
|
|
|
|
* Title: Page not Found
|
|
|
|
* ErrorCode: 404
|
|
|
|
* </code>
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-11 01:17:51 +02:00
|
|
|
* @subpackage core
|
|
|
|
*
|
2011-02-01 21:05:47 +01:00
|
|
|
* @see http://code.google.com/p/spyc/
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
|
|
|
* @todo Write unit test for YamlFixture
|
|
|
|
*/
|
|
|
|
class YamlFixture extends Object {
|
|
|
|
|
|
|
|
/**
|
2011-03-30 08:47:30 +02:00
|
|
|
* Absolute path to the .yml fixture file
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $fixtureFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of fixture items
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fixtureDictionary;
|
2012-06-29 00:33:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* String containing fixture
|
|
|
|
*
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $fixtureString;
|
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @param String Absolute file path, or relative path to {@link Director::baseFolder()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($fixture) {
|
2012-06-29 00:33:00 +02:00
|
|
|
if(false !== strpos($fixture, "\n")) {
|
|
|
|
$this->fixtureString = $fixture;
|
|
|
|
} else {
|
|
|
|
if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
|
|
|
|
|
|
|
|
if(!file_exists($fixture)) {
|
|
|
|
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture . '" not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fixtureFile = $fixture;
|
2008-08-11 01:29:30 +02:00
|
|
|
}
|
|
|
|
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @return String Absolute file path
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFixtureFile() {
|
2011-03-30 08:47:30 +02:00
|
|
|
return $this->fixtureFile;
|
|
|
|
}
|
2012-06-29 00:33:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String Fixture string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFixtureString() {
|
2012-06-29 00:33:00 +02:00
|
|
|
return $this->fixtureString;
|
|
|
|
}
|
2011-03-30 08:47:30 +02:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* Get the ID of an object from the fixture.
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
|
|
|
*/
|
2008-08-11 05:11:48 +02:00
|
|
|
public function idFromFixture($className, $identifier) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(isset($this->fixtureDictionary[$className][$identifier])) {
|
|
|
|
return $this->fixtureDictionary[$className][$identifier];
|
|
|
|
} else {
|
2009-07-08 04:36:05 +02:00
|
|
|
return false;
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the IDs in the fixture of a particular class name.
|
|
|
|
*
|
|
|
|
* @return A map of fixture-identifier => object-id
|
|
|
|
*/
|
2008-08-11 05:11:48 +02:00
|
|
|
public function allFixtureIDs($className) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(isset($this->fixtureDictionary[$className])) {
|
|
|
|
return $this->fixtureDictionary[$className];
|
|
|
|
} else {
|
2009-07-08 04:36:05 +02:00
|
|
|
return false;
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object from the fixture.
|
|
|
|
*
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
|
|
|
*/
|
2008-08-11 05:11:48 +02:00
|
|
|
public function objFromFixture($className, $identifier) {
|
2008-08-11 07:16:08 +02:00
|
|
|
$id = $this->idFromFixture($className, $identifier);
|
|
|
|
if($id) return DataObject::get_by_id($className, $id);
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a YAML fixture file into the database.
|
2008-10-09 17:14:03 +02:00
|
|
|
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
|
|
|
|
*
|
|
|
|
* Caution: In order to support reflexive relations which need a valid object ID,
|
|
|
|
* the record is written twice: first after populating all non-relational fields,
|
|
|
|
* then again after populating all relations (has_one, has_many, many_many).
|
2008-08-11 01:17:51 +02:00
|
|
|
*/
|
2011-05-01 07:33:02 +02:00
|
|
|
public function saveIntoDatabase(DataModel $model) {
|
2009-07-08 02:06:16 +02:00
|
|
|
// We have to disable validation while we import the fixtures, as the order in
|
|
|
|
// which they are imported doesnt guarantee valid relations until after the
|
|
|
|
// import is complete.
|
|
|
|
$validationenabled = DataObject::get_validation_enabled();
|
|
|
|
DataObject::set_validation_enabled(false);
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
$parser = new Spyc();
|
2012-06-29 00:33:00 +02:00
|
|
|
if (isset($this->fixtureString)) {
|
|
|
|
$fixtureContent = $parser->load($this->fixtureString);
|
|
|
|
} else {
|
|
|
|
$fixtureContent = $parser->loadFile($this->fixtureFile);
|
|
|
|
}
|
2009-11-21 03:33:39 +01:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
$this->fixtureDictionary = array();
|
|
|
|
foreach($fixtureContent as $dataClass => $items) {
|
2008-11-11 00:18:31 +01:00
|
|
|
if(ClassInfo::exists($dataClass)) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->writeDataObject($model, $dataClass, $items);
|
2008-11-11 00:18:31 +01:00
|
|
|
} else {
|
|
|
|
$this->writeSQL($dataClass, $items);
|
|
|
|
}
|
|
|
|
}
|
2009-07-08 02:06:16 +02:00
|
|
|
|
|
|
|
DataObject::set_validation_enabled($validationenabled);
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the fixture into the database using DataObjects
|
|
|
|
*
|
|
|
|
* @param string $dataClass
|
|
|
|
* @param array $items
|
|
|
|
*/
|
2011-05-01 07:33:02 +02:00
|
|
|
protected function writeDataObject($model, $dataClass, $items) {
|
2008-11-11 00:18:31 +01:00
|
|
|
foreach($items as $identifier => $fields) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$obj = $model->$dataClass->newObject();
|
2008-11-11 00:18:31 +01:00
|
|
|
|
|
|
|
// If an ID is explicitly passed, then we'll sort out the initial write straight away
|
|
|
|
// This is just in case field setters triggered by the population code in the next block
|
|
|
|
// Call $this->write(). (For example, in FileTest)
|
|
|
|
if(isset($fields['ID'])) {
|
|
|
|
$obj->ID = $fields['ID'];
|
2010-05-25 05:49:22 +02:00
|
|
|
|
|
|
|
// The database needs to allow inserting values into the foreign key column (ID in our case)
|
|
|
|
$conn = DB::getConn();
|
2010-10-12 23:52:55 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($dataClass), true);
|
2008-11-11 00:18:31 +01:00
|
|
|
$obj->write(false, true);
|
2010-10-12 23:52:55 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($dataClass), false);
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the dictionary with the ID
|
2010-04-13 04:08:08 +02:00
|
|
|
if($fields) foreach($fields as $fieldName => $fieldVal) {
|
2008-11-11 00:18:31 +01:00
|
|
|
if($obj->many_many($fieldName) || $obj->has_many($fieldName) || $obj->has_one($fieldName)) continue;
|
|
|
|
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
|
|
|
|
}
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
// has to happen before relations in case a class is referring to itself
|
|
|
|
$this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;
|
|
|
|
|
|
|
|
// Populate all relations
|
2010-04-13 04:08:08 +02:00
|
|
|
if($fields) foreach($fields as $fieldName => $fieldVal) {
|
2008-11-11 00:18:31 +01:00
|
|
|
if($obj->many_many($fieldName) || $obj->has_many($fieldName)) {
|
|
|
|
$parsedItems = array();
|
2009-06-18 11:34:17 +02:00
|
|
|
$items = preg_split('/ *, */',trim($fieldVal));
|
2008-11-11 00:18:31 +01:00
|
|
|
foreach($items as $item) {
|
|
|
|
$parsedItems[] = $this->parseFixtureVal($item);
|
|
|
|
}
|
|
|
|
$obj->write();
|
|
|
|
if($obj->has_many($fieldName)) {
|
|
|
|
$obj->getComponents($fieldName)->setByIDList($parsedItems);
|
|
|
|
} elseif($obj->many_many($fieldName)) {
|
|
|
|
$obj->getManyManyComponents($fieldName)->setByIDList($parsedItems);
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
2008-11-11 00:18:31 +01:00
|
|
|
} elseif($obj->has_one($fieldName)) {
|
2009-09-17 05:24:15 +02:00
|
|
|
$obj->{$fieldName . 'ID'} = $this->parseFixtureVal($fieldVal);
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-11 00:18:31 +01:00
|
|
|
$obj->write();
|
2012-06-29 00:33:00 +02:00
|
|
|
//If LastEdited was set in the fixture, set it here
|
|
|
|
if (array_key_exists('LastEdited', $fields)) {
|
|
|
|
$manip = array($dataClass => array("command" => "update", "id" => $obj->id,
|
|
|
|
"fields" => array("LastEdited" => "'".$this->parseFixtureVal($fields['LastEdited'])."'")));
|
|
|
|
DB::manipulate($manip);
|
|
|
|
}
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the fixture into the database directly using a database manipulation
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $items
|
|
|
|
*/
|
|
|
|
protected function writeSQL($table, $items) {
|
|
|
|
foreach($items as $identifier => $fields) {
|
|
|
|
$manipulation = array($table => array("fields" => array(), "command" => "insert"));
|
|
|
|
foreach($fields as $fieldName=> $fieldVal) {
|
|
|
|
$manipulation[$table]["fields"][$fieldName] = "'".$this->parseFixtureVal($fieldVal)."'";
|
|
|
|
}
|
|
|
|
DB::manipulate($manipulation);
|
|
|
|
$this->fixtureDictionary[$table][$identifier] = DB::getGeneratedID($table);
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a value from a fixture file. If it starts with => it will get an ID from the fixture dictionary
|
|
|
|
*/
|
|
|
|
protected function parseFixtureVal($fieldVal) {
|
|
|
|
// Parse a dictionary reference - used to set foreign keys
|
|
|
|
if(substr($fieldVal,0,2) == '=>') {
|
|
|
|
list($a, $b) = explode('.', substr($fieldVal,2), 2);
|
|
|
|
return $this->fixtureDictionary[$a][$b];
|
|
|
|
|
|
|
|
// Regular field value setting
|
|
|
|
} else {
|
|
|
|
return $fieldVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|