RelationshipsTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Elgg\Database;
  3. class RelationshipsTest extends \PHPUnit_Framework_TestCase {
  4. private $guids;
  5. /**
  6. * Create the entities used for each test
  7. */
  8. protected function setUp() {
  9. $this->markTestSkipped("Skipped test as Elgg can not yet run PHP Unit tests that interact with the database");
  10. $this->guids = array();
  11. $obj1 = new \ElggObject();
  12. $obj1->save();
  13. $this->guids[] = $obj1->guid;
  14. $obj2 = new \ElggObject();
  15. $obj2->save();
  16. $this->guids[] = $obj2->guid;
  17. }
  18. /**
  19. * Remove the entities that are created for each test
  20. */
  21. protected function tearDown() {
  22. foreach ($this->guids as $guid) {
  23. $e = get_entity($guid);
  24. $e->delete();
  25. }
  26. }
  27. /**
  28. * Check that you can get entities by filtering them on relationship time created lower
  29. */
  30. public function testGetEntitiesFromRelationshipFilterByTimeCreatedLower() {
  31. // get a timestamp before creating the relationship
  32. $ts_lower = time() - 1;
  33. add_entity_relationship($this->guids[0], 'testGetEntitiesFromRelationship', $this->guids[1]);
  34. // get a timestamp after creating the relationship
  35. $ts_upper = time() + 1;
  36. // check that if ts_lower is before the relationship you get the just created entity
  37. $options = array(
  38. 'relationship' => 'testGetEntitiesFromRelationship',
  39. 'relationship_guid' => $this->guids[0],
  40. 'relationship_created_time_lower' => $ts_lower
  41. );
  42. $es = elgg_get_entities_from_relationship($options);
  43. $this->assertTrue(is_array($es));
  44. $this->assertIdentical(count($es), 1);
  45. foreach ($es as $e) {
  46. $this->assertEqual($this->guids[1], $e->guid);
  47. }
  48. // check that if ts_lower is after the relationship you get no entities
  49. $options = array(
  50. 'relationship' => 'testGetEntitiesFromRelationship',
  51. 'relationship_guid' => $this->guids[0],
  52. 'relationship_created_time_lower' => $ts_upper
  53. );
  54. $es = elgg_get_entities_from_relationship($options);
  55. $this->assertTrue(is_array($es));
  56. $this->assertIdentical(count($es), 0);
  57. }
  58. /**
  59. * Check that you can get entities by filtering them on relationship time created upper
  60. */
  61. public function testGetEntitiesFromRelationshipFilterByTimeCreatedUpper() {
  62. // get a timestamp before creating the relationship
  63. $ts_lower = time() - 1;
  64. add_entity_relationship($this->guids[0], 'testGetEntitiesFromRelationship', $this->guids[1]);
  65. // get a timestamp after creating the relationship
  66. $ts_upper = time() + 1;
  67. // check that if ts_upper is after the relationship you get the just created entity
  68. $options = array(
  69. 'relationship' => 'testGetEntitiesFromRelationship',
  70. 'relationship_guid' => $this->guids[0],
  71. 'relationship_created_time_upper' => $ts_upper
  72. );
  73. $es = elgg_get_entities_from_relationship($options);
  74. $this->assertTrue(is_array($es));
  75. $this->assertIdentical(count($es), 1);
  76. foreach ($es as $e) {
  77. $this->assertEqual($this->guids[1], $e->guid);
  78. }
  79. // check that if ts_upper is before the relationship you get no entities
  80. $options = array(
  81. 'relationship' => 'testGetEntitiesFromRelationship',
  82. 'relationship_guid' => $this->guids[0],
  83. 'relationship_created_time_upper' => $ts_lower
  84. );
  85. $es = elgg_get_entities_from_relationship($options);
  86. $this->assertTrue(is_array($es));
  87. $this->assertIdentical(count($es), 0);
  88. }
  89. /**
  90. * Check that you can get entities by filtering them on relationship time created lower and upper
  91. */
  92. public function testGetEntitiesFromRelationshipFilterByTimeCreatedLowerAndUpper() {
  93. // get a timestamp before creating the relationship
  94. $ts_lower = time() - 1;
  95. add_entity_relationship($this->guids[0], 'testGetEntitiesFromRelationship', $this->guids[1]);
  96. // get a timestamp after creating the relationship
  97. $ts_upper = time() + 1;
  98. // check that if relationship time created is between lower and upper you get the just created entity
  99. $options = array(
  100. 'relationship' => 'testGetEntitiesFromRelationship',
  101. 'relationship_guid' => $this->guids[0],
  102. 'relationship_created_time_lower' => $ts_lower,
  103. 'relationship_created_time_upper' => $ts_upper
  104. );
  105. $es = elgg_get_entities_from_relationship($options);
  106. $this->assertTrue(is_array($es));
  107. $this->assertIdentical(count($es), 1);
  108. foreach ($es as $e) {
  109. $this->assertEqual($this->guids[1], $e->guid);
  110. }
  111. // check that if ts_lower > ts_upper you get no entities
  112. $options = array(
  113. 'relationship' => 'testGetEntitiesFromRelationship',
  114. 'relationship_guid' => $this->guids[0],
  115. 'relationship_created_time_lower' => $ts_upper,
  116. 'relationship_created_time_upper' => $ts_lower
  117. );
  118. $es = elgg_get_entities_from_relationship($options);
  119. $this->assertTrue(is_array($es));
  120. $this->assertIdentical(count($es), 0);
  121. }
  122. }