Example #1
0
 public function testHasOnes()
 {
     $obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
     $image1 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image1');
     $image2 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image2');
     $relobj1 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj1');
     $relobj2 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj2');
     // in order to ensure the Filename path is correct, append the correct FRAMEWORK_DIR to the start
     // this is only really necessary to make the test pass when FRAMEWORK_DIR is not "framework"
     $image1->Filename = FRAMEWORK_DIR . substr($image1->Filename, 9);
     $image2->Filename = FRAMEWORK_DIR . substr($image2->Filename, 9);
     $origUpdateFilesystem = File::$update_filesystem;
     // we don't want the filesystem being updated on write, as we're only dealing with mock files
     File::$update_filesystem = false;
     $image1->write();
     $image2->write();
     File::$update_filesystem = $origUpdateFilesystem;
     // create a new version
     $obj1->ImageID = $image2->ID;
     $obj1->HasOneRelationID = $relobj2->ID;
     $obj1->write();
     $obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version - 1);
     $obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version);
     $differ = new DataDifferencer($obj1v1, $obj1v2);
     $obj1Diff = $differ->diffedData();
     $this->assertContains($image1->Filename, $obj1Diff->getField('Image'));
     $this->assertContains($image2->Filename, $obj1Diff->getField('Image'));
     $this->assertContains('<ins>obj2</ins><del>obj1</del>', str_replace(' ', '', $obj1Diff->getField('HasOneRelationID')));
 }
 protected function failSafe()
 {
     if (!$this->isValid()) {
         $this->owner->setFilename(self::$fallback_image);
         $state = File::$update_filesystem;
         File::$update_filesystem = false;
         $this->owner->write();
         File::$update_filesystem = $state;
     }
 }
 /**
  * Mostly rewritten from parent, with changes that allow us to update objects (not only write new).
  * Also adds dummy file creation. The files will either be empty (touched only), or will be copied
  * from the testdata directory if found.
  */
 protected function writeDataObject($model, $dataClass, $items)
 {
     File::$update_filesystem = false;
     if (Director::isLive()) {
         user_error('This should not be run on the live site.', E_USER_ERROR);
     }
     $testDataTag = basename($this->fixtureFile);
     foreach ($items as $identifier => $fields) {
         $tag = $this->getTag($testDataTag, $dataClass, $identifier);
         $obj = null;
         if ($tag) {
             $obj = $tag->getObject();
         }
         // Create the object
         if (!isset($obj) || !$obj) {
             if ($tag) {
                 $tag->delete();
                 // Tag not valid, delete.
                 $tag = null;
             }
             $obj = $model->{$dataClass}->newObject();
         }
         // 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)
         // Do this only if the object is new.
         if (!$tag && isset($fields['ID']) && is_array($fields)) {
             $obj->ID = $fields['ID'];
             // The database needs to allow inserting values into the foreign key column (ID in our case)
             $conn = DB::getConn();
             if (method_exists($conn, 'allowPrimaryKeyEditing')) {
                 $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($dataClass), true);
             }
             $obj->write(false, true);
             if (method_exists($conn, 'allowPrimaryKeyEditing')) {
                 $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($dataClass), false);
             }
         }
         // Populate the dictionary with the ID
         if (!is_array($fields)) {
             throw new Exception($dataClass . ' failed to load. Please check YML file for errors');
         }
         if ($fields) {
             foreach ($fields as $fieldName => $fieldVal) {
                 if ($obj->many_many($fieldName) || $obj->has_many($fieldName) || $obj->has_one($fieldName)) {
                     continue;
                 }
                 $obj->{$fieldName} = $this->parseFixtureVal($fieldVal);
             }
         }
         $obj->write();
         // when creating a Folder record, the directory should exist
         if (is_a($obj, 'Folder')) {
             if (!file_exists($obj->FullPath)) {
                 mkdir($obj->FullPath);
             }
             chmod($obj->FullPath, Filesystem::$file_create_mask);
         }
         // when creating a File record, the file should exist
         if (is_a($obj, 'File')) {
             // Create the directory.
             $dirPath = substr($obj->getFullPath(), 0, strrpos($obj->getFullPath(), '/'));
             @mkdir($dirPath, 0777, true);
             // Make sure the file exists.
             touch($obj->FullPath);
             // if there is a dummy file of the same name in a testdata dir, put it's contents into the newly created assets path
             // @todo what priority do we set if test files are found in modules versus project code in mysite?
             $result = glob(sprintf(BASE_PATH . '/*/testdata/files/%s', $obj->Name));
             if ($result) {
                 file_put_contents($obj->FullPath, file_get_contents($result[0]));
             }
             chmod($obj->FullPath, Filesystem::$file_create_mask);
         }
         // has to happen before relations in case a class is referring to itself
         $this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;
         TestDataYamlFixture::attempt_publish($obj);
         // Increment the version on the tag so we can find the old unused records afterwards.
         if ($tag) {
             $tag->Version = $this->latestVersion + 1;
             $tag->write();
         } else {
             $tag = $model->TestDataTag->newObject();
             $tag->Class = $dataClass;
             $tag->RecordID = $obj->ID;
             $tag->FixtureFile = $testDataTag;
             $tag->FixtureID = $identifier;
             $tag->Version = $this->latestVersion + 1;
             $tag->write();
         }
         echo '.';
     }
 }