ElggCoreGetEntitiesFromAttributesTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Test elgg_get_entities_from_attributes()
  4. */
  5. class ElggCoreGetEntitiesFromAttributesTest extends \ElggCoreGetEntitiesBaseTest {
  6. public function testWithoutType() {
  7. $this->expectException(new \InvalidArgumentException('The entity type must be defined for elgg_get_entities_from_attributes()'));
  8. elgg_get_entities_from_attributes();
  9. }
  10. public function testWithMoreThanOneType() {
  11. $this->expectException(new \InvalidArgumentException('Only one type can be passed to elgg_get_entities_from_attributes()'));
  12. elgg_get_entities_from_attributes(array('types' => array('one', 'two')));
  13. }
  14. public function testWithInvalidType() {
  15. $this->expectException(new \InvalidArgumentException("Invalid type 'test' passed to elgg_get_entities_from_attributes()"));
  16. elgg_get_entities_from_attributes(array('type' => 'test'));
  17. }
  18. public function testWithInvalidPair() {
  19. $this->expectException(new \InvalidArgumentException("attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()"));
  20. elgg_get_entities_from_attributes(array(
  21. 'types' => 'object',
  22. 'attribute_name_value_pairs' => 'invalid',
  23. ));
  24. }
  25. public function testGetSqlWithNoAttributePairs() {
  26. $result = _elgg_get_entity_attribute_where_sql(array(
  27. 'types' => 'object',
  28. 'attribute_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
  29. ));
  30. $this->assertIdentical(array('joins' => array(), 'wheres' => array()), $result);
  31. }
  32. public function testGetSqlWithEmptyPairs() {
  33. $result = _elgg_get_entity_attribute_where_sql(array(
  34. 'types' => 'object',
  35. 'attribute_name_value_pairs' => array(),
  36. ));
  37. $this->assertIdentical(array('joins' => array(), 'wheres' => array()), $result);
  38. }
  39. public function testGetSqlWithSinglePairAndStringValue() {
  40. $result = _elgg_get_entity_attribute_where_sql(array(
  41. 'types' => 'object',
  42. 'attribute_name_value_pairs' => array(
  43. 'name' => 'title',
  44. 'value' => 'foo',
  45. ),
  46. 'attribute_name_value_pairs_operator' => 'AND',
  47. ));
  48. global $CONFIG;
  49. $expected = array(
  50. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  51. 'wheres' => array("((type_table.title = 'foo'))"),
  52. );
  53. $this->assertIdentical($expected, $result);
  54. }
  55. public function testGetSqlWithSinglePairAndOperand() {
  56. $result = _elgg_get_entity_attribute_where_sql(array(
  57. 'types' => 'object',
  58. 'attribute_name_value_pairs' => array(
  59. 'name' => 'title',
  60. 'value' => 'foo',
  61. 'operand' => '<',
  62. ),
  63. 'attribute_name_value_pairs_operator' => 'AND',
  64. ));
  65. global $CONFIG;
  66. $expected = array(
  67. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  68. 'wheres' => array("((type_table.title < 'foo'))"),
  69. );
  70. $this->assertIdentical($expected, $result);
  71. }
  72. public function testGetSqlWithSinglePairAndNumericValue() {
  73. $result = _elgg_get_entity_attribute_where_sql(array(
  74. 'types' => 'object',
  75. 'attribute_name_value_pairs' => array(
  76. 'name' => 'title',
  77. 'value' => '32',
  78. ),
  79. 'attribute_name_value_pairs_operator' => 'AND',
  80. ));
  81. global $CONFIG;
  82. $expected = array(
  83. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  84. 'wheres' => array("((type_table.title = 32))"),
  85. );
  86. $this->assertIdentical($expected, $result);
  87. }
  88. public function testGetSqlWithSinglePairAndNumericArrayValue() {
  89. $result = _elgg_get_entity_attribute_where_sql(array(
  90. 'types' => 'object',
  91. 'attribute_name_value_pairs' => array(
  92. 'name' => 'title',
  93. 'value' => array(1, 2, 3),
  94. ),
  95. 'attribute_name_value_pairs_operator' => 'AND',
  96. ));
  97. global $CONFIG;
  98. $expected = array(
  99. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  100. 'wheres' => array("((type_table.title IN (1, 2, 3)))"),
  101. );
  102. $this->assertIdentical($expected, $result);
  103. }
  104. public function testGetSqlWithSinglePairAndStringArrayValue() {
  105. $result = _elgg_get_entity_attribute_where_sql(array(
  106. 'types' => 'object',
  107. 'attribute_name_value_pairs' => array(
  108. 'name' => 'title',
  109. 'value' => array('one', 'two'),
  110. ),
  111. 'attribute_name_value_pairs_operator' => 'AND',
  112. ));
  113. global $CONFIG;
  114. $expected = array(
  115. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  116. 'wheres' => array("((type_table.title IN ('one', 'two')))"),
  117. );
  118. $this->assertIdentical($expected, $result);
  119. }
  120. public function testGetSqlWithTwoPairs() {
  121. $result = _elgg_get_entity_attribute_where_sql(array(
  122. 'types' => 'user',
  123. 'attribute_name_value_pairs' => array(
  124. array('name' => 'username', 'value' => 'user2'),
  125. array('name' => 'email', 'value' => 'test@example.org'),
  126. ),
  127. 'attribute_name_value_pairs_operator' => 'AND',
  128. ));
  129. global $CONFIG;
  130. $expected = array(
  131. 'joins' => array("JOIN {$CONFIG->dbprefix}users_entity type_table ON e.guid = type_table.guid"),
  132. 'wheres' => array("((type_table.username = 'user2') AND (type_table.email = 'test@example.org'))"),
  133. );
  134. $this->assertIdentical($expected, $result);
  135. }
  136. public function testGetSqlWithSinglePairAndCaseInsensitiveStringValue() {
  137. $result = _elgg_get_entity_attribute_where_sql(array(
  138. 'types' => 'object',
  139. 'attribute_name_value_pairs' => array(
  140. 'name' => 'title',
  141. 'value' => 'foo',
  142. 'case_sensitive' => true,
  143. ),
  144. 'attribute_name_value_pairs_operator' => 'AND',
  145. ));
  146. global $CONFIG;
  147. $expected = array(
  148. 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
  149. 'wheres' => array("((BINARY type_table.title = 'foo'))"),
  150. );
  151. $this->assertIdentical($expected, $result);
  152. }
  153. public function testGetUserByUsername() {
  154. // grab a user
  155. foreach ($this->entities as $e) {
  156. if (elgg_instanceof($e, 'user')) {
  157. break;
  158. }
  159. }
  160. $result = elgg_get_entities_from_attributes(array(
  161. 'type' => 'user',
  162. 'attribute_name_value_pairs' => array(
  163. 'name' => 'username',
  164. 'value' => $e->username,
  165. ),
  166. ));
  167. $this->assertEqual($e->guid, $result[0]->guid);
  168. }
  169. }