diff --git a/includes/WdEntityWrapper.php b/includes/WdEntityWrapper.php index d6db9ed..47e4892 100644 --- a/includes/WdEntityWrapper.php +++ b/includes/WdEntityWrapper.php @@ -287,4 +287,17 @@ class WdEntityWrapper { return new $class($this->value()->original); } + /** + * Checks if a field changed after entity update. + * Warning: does not currently work consistently with formatted text fields + * and text with summary fields. (Drupal adds missing array keys during + * save process) + * + * @param $name + * @return boolean + */ + public function isChanged($name) { + return $this->get($name) != $this->getOriginal()->get($name); + } + } diff --git a/tests/wrappers_delight.test b/tests/wrappers_delight.test index 692eee2..4998d11 100644 --- a/tests/wrappers_delight.test +++ b/tests/wrappers_delight.test @@ -217,5 +217,21 @@ class WrappersDelightEntityHooksTestCase extends WrappersDelightTestCase { $this->assertEqual($wd_test_results['node']['presave']['original']['body'], $body[LANGUAGE_NONE][0]['value']); $this->assertEqual($wd_test_results['node']['update']['original']['title'], $title); $this->assertEqual($wd_test_results['node']['update']['original']['body'], $body[LANGUAGE_NONE][0]['value']); + // Test is changed + $this->assertTrue($wd_test_results['node']['presave']['is_changed']['title']); + $this->assertTrue($wd_test_results['node']['presave']['is_changed']['body']); + $this->assertFalse($wd_test_results['node']['presave']['is_changed']['created']); + $this->assertTrue($wd_test_results['node']['update']['is_changed']['title']); + $this->assertTrue($wd_test_results['node']['update']['is_changed']['body']); + $this->assertFalse($wd_test_results['node']['update']['is_changed']['created']); + + $node->title = 'Yet another title.'; + node_save($node); + $this->assertTrue($wd_test_results['node']['presave']['is_changed']['title']); + $this->assertFalse($wd_test_results['node']['presave']['is_changed']['body']); + $this->assertFalse($wd_test_results['node']['presave']['is_changed']['created']); + $this->assertTrue($wd_test_results['node']['update']['is_changed']['title']); + $this->assertFalse($wd_test_results['node']['update']['is_changed']['body']); + $this->assertFalse($wd_test_results['node']['update']['is_changed']['created']); } } \ No newline at end of file diff --git a/tests/wrappers_delight_test_module.module b/tests/wrappers_delight_test_module.module index 04e4866..57b74af 100644 --- a/tests/wrappers_delight_test_module.module +++ b/tests/wrappers_delight_test_module.module @@ -15,6 +15,9 @@ function wrappers_delight_test_module_entity_presave($entity, $type) { $wd_test_results['node']['presave']['is_new'] = $wrapped->isNew(); $wd_test_results['node']['presave']['original']['title'] = $wrapped->getOriginal()->getTitle(); $wd_test_results['node']['presave']['original']['body'] = $wrapped->getOriginal()->getText('body', WdEntityWrapper::FORMAT_PLAIN); + $wd_test_results['node']['presave']['is_changed']['title'] = $wrapped->isChanged('title'); + $wd_test_results['node']['presave']['is_changed']['body'] = $wrapped->isChanged('body'); + $wd_test_results['node']['presave']['is_changed']['created'] = $wrapped->isChanged('created'); } } @@ -41,5 +44,8 @@ function wrappers_delight_test_module_entity_update($entity, $type) { $wd_test_results['node']['update']['is_new'] = $wrapped->isNew(); $wd_test_results['node']['update']['original']['title'] = $wrapped->getOriginal()->getTitle(); $wd_test_results['node']['update']['original']['body'] = $wrapped->getOriginal()->getText('body', WdEntityWrapper::FORMAT_PLAIN); + $wd_test_results['node']['update']['is_changed']['title'] = $wrapped->isChanged('title'); + $wd_test_results['node']['update']['is_changed']['body'] = $wrapped->isChanged('body'); + $wd_test_results['node']['update']['is_changed']['created'] = $wrapped->isChanged('created'); } } \ No newline at end of file