Permalink
Browse files

fix(relationships): can now prevent relationships using event handler

Passes the relationship id into delete_relationship() instead of the event handler return

Fixes #8927
  • Loading branch information...
beck24 committed Sep 13, 2015
1 parent 6ba6fa2 commit 9a275d9c9dce532a8a837a758e5fa241f02e2246
@@ -105,11 +105,14 @@ User events
Relationship events
===================
**create, <relationship>**
**create, relationship**
Triggered after a relationship has been created. Returning false deletes
the relationship that was just created.
**delete, <relationship>**
.. note:: This event was broken in Elgg 1.9 - 1.12.3, returning false would *not*
delete the relationship. This is working as of 1.12.4
**delete, relationship**
Triggered before a relationship is deleted. Return false to prevent it
from being deleted.
@@ -124,7 +124,7 @@ function add($guid_one, $relationship, $guid_two) {
if ($result && $result_old) {
return true;
} else {
delete_relationship($result);
delete_relationship($obj->id);
}
}
@@ -279,3 +279,31 @@ function elgg_get_entities_from_relationship_count(array $options = array()) {
function elgg_list_entities_from_relationship_count($options) {
return elgg_list_entities($options, 'elgg_get_entities_from_relationship_count');
}
/**
* Register relationship unit tests
*
* @param string $hook
* @param string $type
* @param array $tests
* @return array
* @access private
*/
function _elgg_relationships_test($hook, $type, $tests) {
global $CONFIG;
$tests[] = $CONFIG->path . 'engine/tests/ElggRelationshipTest.php';
return $tests;
}
/**
* Initialize the relationship library
* @access private
*/
function _elgg_relationship_init() {
elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_relationships_test');
}
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
$events->registerHandler('init', 'system', '_elgg_relationship_init');
};
@@ -0,0 +1,234 @@
<?php
/**
* test ElggRelationship
*
* @package Elgg
* @subpackage Test
*/
class ElggRelationshipTest extends ElggCoreUnitTest {
/**
* @var ElggEntity
*/
protected $entity1;
protected $entity2;
protected $entity3;
/**
* Called before each test method.
*/
public function setUp() {
$this->original_events = _elgg_services()->events;
_elgg_services()->events = new Elgg\EventsService();
$this->entity1 = new ElggObject();
$this->entity1->subtype = 'elgg_relationship_test';
$this->entity1->access_id = ACCESS_PUBLIC;
$this->entity1->save();
$this->entity2 = new ElggObject();
$this->entity2->subtype = 'elgg_relationship_test';
$this->entity2->access_id = ACCESS_PUBLIC;
$this->entity2->save();
$this->entity3 = new ElggObject();
$this->entity3->subtype = 'elgg_relationship_test';
$this->entity3->access_id = ACCESS_PUBLIC;
$this->entity3->save();
}
/**
* Called after each test method.
*/
public function tearDown() {
if ($this->entity1) {
$this->entity1->delete();
}
if ($this->entity2) {
$this->entity2->delete();
}
if ($this->entity3) {
$this->entity3->delete();
}
remove_subtype('object', 'elgg_relationship_test');
_elgg_services()->events = $this->original_events;
}
/**
* Tests
*/
public function testAddRelationship() {
// test adding a relationship
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
}
public function testRemoveRelationship() {
// test adding a relationship
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue(remove_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
}
public function testPreventAddRelationship() {
// test event handler - should prevent the addition of a relationship
elgg_register_event_handler('create', 'relationship', 'Elgg\Values::getFalse');
$this->assertFalse(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
elgg_unregister_event_handler('create', 'relationship', 'Elgg\Values::getFalse');
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
}
public function testPreventRemoveRelationship() {
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
elgg_register_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
$this->assertFalse(remove_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
elgg_unregister_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
}
public function testRelationshipSave() {
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
// note - string because that's how it's returned when getting a new object
$r->guid_two = (string) $this->entity3->guid;
$this->assertTrue($r->save());
$test_r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
$this->assertIsA($test_r, 'ElggRelationship');
$this->assertIdentical($r->guid_one, $test_r->guid_one);
$this->assertIdentical($r->guid_two, $test_r->guid_two);
$this->assertIdentical($r->relationship, $test_r->relationship);
// the original shouldn't exist anymore
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
}
public function testRelationshipDelete() {
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($r->delete());
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
}
public function testRelationshipOnEntityDelete() {
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
// test deleting entity in guid_one position
$this->entity1->delete();
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$this->assertTrue(add_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity3->guid));
// test deleting entity in guid_two position
$this->entity3->delete();
$this->assertFalse(check_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity3->guid));
}
public function testPreventRelationshipOnEntityDelete() {
$this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$this->assertTrue(add_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity1->guid));
$guid = $this->entity1->guid;
elgg_register_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
$this->assertTrue($this->entity1->delete());
elgg_unregister_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
// relationships should still be gone as there is no entity
// despite the fact we have a handler trying to prevent it
$this->assertFalse(check_entity_relationship($guid, 'test_relationship', $this->entity2->guid));
$this->assertFalse(check_entity_relationship($this->entity2->guid, 'test_relationship', $guid));
}
public function testEntityMethodAddRelationship() {
$this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
}
public function testEntityMethodRemoveRelationship() {
$this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->removeRelationship($this->entity2->guid, 'test_relationship'));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
}
public function testEntityMethodDeleteRelationships() {
$this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->deleteRelationships('test_relationship'));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
// inverse relationships should be gone too
$this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
// Repeat above test, but with no relationship - should remove all relationships
$this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
$r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship2'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship2'));
$r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid);
$this->assertIsA($r, 'ElggRelationship');
$this->assertTrue($this->entity1->deleteRelationships());
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid));
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid));
// inverse relationships should be gone too
$this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
}
}

0 comments on commit 9a275d9

Please sign in to comment.