From 5542910ca1d25eca8d5eda779fedabb0f8f7c759 Mon Sep 17 00:00:00 2001 From: Simon Bouland Date: Fri, 5 May 2017 15:04:49 +0200 Subject: [PATCH 1/2] Expose preexistent logic in public function getVersionId --- src/Model/Behavior/VersionBehavior.php | 43 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/Model/Behavior/VersionBehavior.php b/src/Model/Behavior/VersionBehavior.php index bcb8554..27397af 100644 --- a/src/Model/Behavior/VersionBehavior.php +++ b/src/Model/Behavior/VersionBehavior.php @@ -167,29 +167,17 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o $fields = $this->_fields(); $values = $entity->extract($fields, $this->_config['onlyDirty']); - $model = $this->_config['referenceName']; $primaryKey = (array)$this->_table->primaryKey(); - $foreignKey = $this->_extractForeignKey($entity); $versionField = $this->_config['versionField']; if (isset($options['versionId'])) { $versionId = $options['versionId']; } else { - $table = TableRegistry::get($this->_config['versionTable']); - $preexistent = $table->find() - ->select(['version_id']) - ->where([ - 'model' => $model - ] + $foreignKey) - ->order(['id desc']) - ->limit(1) - ->hydrate(false) - ->toArray(); - - $versionId = Hash::get($preexistent, '0.version_id', 0) + 1; + $versionId = $this->getVersionId($entity) + 1; } $created = new DateTime(); $new = []; + $entityClass = TableRegistry::get($this->_config['versionTable'])->entityClass(); foreach ($values as $field => $content) { if (in_array($field, $primaryKey) || $field == $versionField) { continue; @@ -197,11 +185,11 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o $data = [ 'version_id' => $versionId, - 'model' => $model, + 'model' => $this->_config['referenceName'], 'field' => $field, 'content' => $content, 'created' => $created, - ] + $foreignKey; + ] + $this->_extractForeignKey($entity); $event = new Event('Model.Version.beforeSave', $this, $options); $userData = EventManager::instance()->dispatch($event); @@ -209,7 +197,6 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o $data = array_merge($data, $userData->result); } - $entityClass = $table->entityClass(); $new[$field] = new $entityClass($data, [ 'useSetters' => false, 'markNew' => true @@ -235,6 +222,28 @@ public function afterSave(Event $event, EntityInterface $entity) $entity->unsetProperty($property); } + /** + * return the last version id + * + * @param \Cake\Datasource\EntityInterface $entity Entity. + * @return int + */ + public function getVersionId(EntityInterface $entity) + { + $table = TableRegistry::get($this->_config['versionTable']); + $preexistent = $table->find() + ->select(['version_id']) + ->where([ + 'model' => $this->_config['referenceName'] + ] + $this->_extractForeignKey($entity)) + ->order(['id desc']) + ->limit(1) + ->hydrate(false) + ->toArray(); + + return Hash::get($preexistent, '0.version_id', 0); + } + /** * Custom finder method used to retrieve all versions for the found records. * From 18f6784b21d09ae6bbeae04e772860a42ff5b8e7 Mon Sep 17 00:00:00 2001 From: Simon Bouland Date: Tue, 9 May 2017 16:18:15 +0200 Subject: [PATCH 2/2] Add test for method getVersionId --- .../Model/Behavior/VersionBehaviorTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/TestCase/Model/Behavior/VersionBehaviorTest.php b/tests/TestCase/Model/Behavior/VersionBehaviorTest.php index 594cd12..85bfcd9 100644 --- a/tests/TestCase/Model/Behavior/VersionBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/VersionBehaviorTest.php @@ -369,4 +369,25 @@ public function testAssociations() $this->assertInstanceOf('Cake\Orm\Association\HasMany', $bodyVersions); $this->assertEquals('body_version', $bodyVersions->property()); } + + /** + * @return void + */ + public function testGetVersionId() + { + // init test data + $table = TableRegistry::get('Articles', [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + ]); + $table->addBehavior('Josegonzalez/Version.Version'); + $article = $table->find('all')->where(['version_id' => 2])->first(); + $article->title = 'First Article Version 3'; + $table->save($article); + + // action in controller receiving outdated data + $table->patchEntity($article, ['version_id' => 2]); + + $this->assertEquals(2, $article->version_id); + $this->assertEquals(3, $table->getVersionId($article)); + } }