From 6e13222ad1fb64fc459332aa711e98e0c407fbd3 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Tue, 13 Feb 2018 21:41:00 -0200 Subject: [PATCH] do not get thumb from entity with no post ID, add diff function to entities and repositories, simple collection diff test --- .../entities/class-tainacan-entity.php | 10 +++++ .../class-tainacan-repository.php | 45 ++++++++++++++++++- tests/test-collections.php | 25 +++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index be1b2931f..9280790eb 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -361,4 +361,14 @@ class Entity { return get_post_type_capabilities((object) $args); } + /** + * Compare this entity props with self old values or with $which other entity + * @param Entity|integer|\WP_Post $which default ($which = 0) to self compare with stored entity + * @return array + */ + public function diff($which = 0) { + global ${$this->repository}; + return ${$this->repository}->diff($which, $this); + } + } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 64eb80165..4cb96d9d7 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -285,7 +285,9 @@ abstract class Repository { $property = get_term_meta($entity->WP_Term->term_id, $prop, true); } elseif ( isset( $entity->WP_Post )) { if($mapped == 'thumbnail'){ - $property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full'); + if(isset($entity->WP_Post->ID)) { + $property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full'); + } } else { $property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null; } @@ -317,7 +319,7 @@ abstract class Repository { /** * - * @param integer|\WP_Post $post + * @param integer|\WP_Post $post|Entity * @throws \Exception * @return \Tainacan\Entities\Entity|boolean */ @@ -503,6 +505,45 @@ abstract class Repository { return user_can($user, $entity_cap->publish_posts, $entity->get_id()); } + /** + * Compare two repository entities + * + * @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity + * @param Entity|integer|\WP_Post $new + * + * @return array List of diff values + */ + public function diff($old = 0, $new) { + $old_entity = null; + if($old === 0) { // self diff or other entity? + $id = $new->get_id(); + if(!empty($id)) { // there is a repository entity? + $old_entity = $this->get_entity_by_post($new->WP_Post->ID); + } + else { + $entity_type = get_class($new); + $old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one + } + } + else { // get entity from repository + $old_entity = $this->get_entity_by_post($old); + } + $new_entity = $this->get_entity_by_post($new); + + $map = $this->get_map(); + + $diff = []; + + foreach ($map as $prop => $mapped) { + if($old_entity->get_mapped_property($prop) != $new_entity->get_mapped_property($prop) ) { + $diff[$prop] = ['new' => $new_entity->get_mapped_property($prop), 'old' => $old_entity->get_mapped_property($prop)]; + } + } + $diff = apply_filters('tainacan-entity-diff', $diff, $new, $old); + + return $diff; + } + } ?> diff --git a/tests/test-collections.php b/tests/test-collections.php index 9796d7554..81b4cf53e 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -219,4 +219,29 @@ class Collections extends TAINACAN_UnitTestCase { global $Tainacan_Collections; $this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!'); } + + /** + * @group diff + */ + function test_diff() { + $x = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testeDiff', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC' + ), + true + ); + + $x->set_name('OtherValue'); + $x->set_description('testeDiff2'); + + $diff = $x->diff(); + $this->assertEquals(2, count($diff)); + $this->assertEquals($diff['name']['new'], 'OtherValue'); + $this->assertEquals($diff['name']['old'], 'testeDiff'); + $this->assertEquals($diff['description']['new'], 'testeDiff2'); + $this->assertEquals($diff['description']['old'], 'adasdasdsa'); + } }