123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- /**
- * Test elgg_get_entities_from_attributes()
- */
- class ElggCoreGetEntitiesFromAttributesTest extends \ElggCoreGetEntitiesBaseTest {
- public function testWithoutType() {
- $this->expectException(new \InvalidArgumentException('The entity type must be defined for elgg_get_entities_from_attributes()'));
- elgg_get_entities_from_attributes();
- }
- public function testWithMoreThanOneType() {
- $this->expectException(new \InvalidArgumentException('Only one type can be passed to elgg_get_entities_from_attributes()'));
- elgg_get_entities_from_attributes(array('types' => array('one', 'two')));
- }
- public function testWithInvalidType() {
- $this->expectException(new \InvalidArgumentException("Invalid type 'test' passed to elgg_get_entities_from_attributes()"));
- elgg_get_entities_from_attributes(array('type' => 'test'));
- }
- public function testWithInvalidPair() {
- $this->expectException(new \InvalidArgumentException("attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()"));
- elgg_get_entities_from_attributes(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => 'invalid',
- ));
- }
- public function testGetSqlWithNoAttributePairs() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
- ));
- $this->assertIdentical(array('joins' => array(), 'wheres' => array()), $result);
- }
- public function testGetSqlWithEmptyPairs() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(),
- ));
- $this->assertIdentical(array('joins' => array(), 'wheres' => array()), $result);
- }
- public function testGetSqlWithSinglePairAndStringValue() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => 'foo',
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.title = 'foo'))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithSinglePairAndOperand() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => 'foo',
- 'operand' => '<',
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.title < 'foo'))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithSinglePairAndNumericValue() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => '32',
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.title = 32))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithSinglePairAndNumericArrayValue() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => array(1, 2, 3),
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.title IN (1, 2, 3)))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithSinglePairAndStringArrayValue() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => array('one', 'two'),
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.title IN ('one', 'two')))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithTwoPairs() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'user',
- 'attribute_name_value_pairs' => array(
- array('name' => 'username', 'value' => 'user2'),
- array('name' => 'email', 'value' => 'test@example.org'),
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}users_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((type_table.username = 'user2') AND (type_table.email = 'test@example.org'))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetSqlWithSinglePairAndCaseInsensitiveStringValue() {
- $result = _elgg_get_entity_attribute_where_sql(array(
- 'types' => 'object',
- 'attribute_name_value_pairs' => array(
- 'name' => 'title',
- 'value' => 'foo',
- 'case_sensitive' => true,
- ),
- 'attribute_name_value_pairs_operator' => 'AND',
- ));
- global $CONFIG;
- $expected = array(
- 'joins' => array("JOIN {$CONFIG->dbprefix}objects_entity type_table ON e.guid = type_table.guid"),
- 'wheres' => array("((BINARY type_table.title = 'foo'))"),
- );
- $this->assertIdentical($expected, $result);
- }
- public function testGetUserByUsername() {
- // grab a user
- foreach ($this->entities as $e) {
- if (elgg_instanceof($e, 'user')) {
- break;
- }
- }
- $result = elgg_get_entities_from_attributes(array(
- 'type' => 'user',
- 'attribute_name_value_pairs' => array(
- 'name' => 'username',
- 'value' => $e->username,
- ),
- ));
- $this->assertEqual($e->guid, $result[0]->guid);
- }
- }
|