Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(relationships): can now prevent relationships using event handler
Passes the relationship id into delete_relationship() instead of the event handler return Fixes Elgg#8927
- Loading branch information
Showing
3 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?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() { | ||
$this->entity1->delete(); | ||
$this->entity2->delete(); | ||
$this->entity3->delete(); | ||
remove_subtype('object', 'elgg_relationship_test'); | ||
|
||
_elgg_services()->events = $this->original_events; | ||
} | ||
|
||
/** | ||
* Tests | ||
*/ | ||
public function testAddRelationship() { | ||
// test adding a relationship | ||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
|
||
$this->assertTrue(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
} | ||
|
||
public function testRemoveRelationship() { | ||
// test adding a relationship | ||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
|
||
$this->assertTrue(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
|
||
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', array($this, 'preventRelationship')); | ||
|
||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
|
||
elgg_unregister_event_handler('create', 'relationship', array($this, 'preventRelationship')); | ||
|
||
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
} | ||
|
||
public function testPreventRemoveRelationship() { | ||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
$this->assertTrue(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
|
||
elgg_register_event_handler('delete', 'relationship', array($this, 'preventRelationship')); | ||
|
||
remove_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
|
||
elgg_unregister_event_handler('delete', 'relationship', array($this, 'preventRelationship')); | ||
|
||
$this->assertTrue(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
} | ||
|
||
public function testRelationshipSave() { | ||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
$relationship = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
$this->assertIsA($relationship, 'ElggRelationship'); | ||
|
||
// note - string because that's how it's returned when getting a new object | ||
$relationship->guid_two = (string) $this->entity3->guid; | ||
$this->assertTrue($relationship->save()); | ||
|
||
$test_relationship = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid); | ||
$this->assertIsA($test_relationship, 'ElggRelationship'); | ||
$this->assertIdentical($relationship->guid_one, $test_relationship->guid_one); | ||
$this->assertIdentical($relationship->guid_two, $test_relationship->guid_two); | ||
$this->assertIdentical($relationship->relationship, $test_relationship->relationship); | ||
|
||
// the original shouldn't exist anymore | ||
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
} | ||
|
||
public function testRelationshipDelete() { | ||
add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
$relationship = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid); | ||
$this->assertIsA($relationship, 'ElggRelationship'); | ||
|
||
$this->assertTrue($relationship->delete()); | ||
$this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid)); | ||
} | ||
|
||
/** | ||
* Helper method for testing relationship prevention via events | ||
* | ||
* @param type $event | ||
* @param type $type | ||
* @param type $relationship | ||
* @return boolean | ||
*/ | ||
public static function preventRelationship($event, $type, $relationship) { | ||
return false; | ||
} | ||
|
||
} |