diff --git a/src/InsightRemoteEntityQuery.inc b/src/InsightRemoteEntityQuery.inc index 17f13a6..f56358f 100644 --- a/src/InsightRemoteEntityQuery.inc +++ b/src/InsightRemoteEntityQuery.inc @@ -309,14 +309,14 @@ class InsightRemoteEntityQuery extends RemoteEntityQuery { // Copy each entity condition. foreach ($efq->entityConditions as $property => $condition) { - // Entity bundle can sometimes be an "IN" operator. - if ($property == 'bundle' && $condition['operator'] == 'IN') { - $condition['value'] = $condition['value'][0]; - } - $this->entityCondition($property, $condition['value'], $condition['operator']); } + // Copy each field condition. + foreach ($efq->fields as $condition) { + $this->fieldCondition($condition['field'], $condition['value']); + } + // Copy the count value. if ($efq->count) { $this->count(); diff --git a/test/src/InsightRemoteEntityQuery.test b/test/src/InsightRemoteEntityQuery.test index 5b4d305..5b93d39 100644 --- a/test/src/InsightRemoteEntityQuery.test +++ b/test/src/InsightRemoteEntityQuery.test @@ -19,6 +19,12 @@ class NewRelicInsightsRemoteEntityQueryTestCase extends DrupalUnitTestCase { protected $query; /** + * A fresh EntityFieldQuery object for this test. + * @var EntityFieldQuery + */ + protected $efq; + + /** * Default NRQL string when no conditions are provided. * @var string */ @@ -48,6 +54,7 @@ class NewRelicInsightsRemoteEntityQueryTestCase extends DrupalUnitTestCase { ), )); $this->query = new InsightRemoteEntityQuery($this->connection); + $this->efq = new EntityFieldQuery(); } /** @@ -91,8 +98,6 @@ class NewRelicInsightsRemoteEntityQueryTestCase extends DrupalUnitTestCase { */ function testBuildNRQL() { $this->query->buildNRQL(); - - $nrql = $this->query->getNRQL(); $this->assertDefaultNRQL('Default NRQL query built successfully.'); } @@ -413,4 +418,228 @@ class NewRelicInsightsRemoteEntityQueryTestCase extends DrupalUnitTestCase { $this->assertIdentical($response, $expected, 'Execute method returned expected results from client for injected NRQL query.'); } + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() count query support. + */ + function testBuildFromEFQCount() { + $this->efq->count(); + $this->query->buildFromEFQ($this->efq); + + $expected_string = 'SELECT COUNT(*) FROM'; + $this->assertNRQLContains($expected_string, 'EFQ-built query supports EFQ::count().'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() range support. + */ + function testBuildFromEFQRange() { + $range = 20; + $this->efq->range(0, $range); + $this->query->buildFromEFQ($this->efq); + + $expected_string = " LIMIT $range"; + $this->assertNRQLContains($expected_string, 'EFQ-built query supports EFQ::range().'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() range support via Views. + */ + function testBuildFromEFQViewsRangePerPage() { + // metaData['view']->query->pager->options['items_per_page']; + $view = (object) array( + 'query' => (object) array( + 'pager' => (object) array( + 'options' => array( + 'items_per_page' => 20, + ), + ), + ), + ); + $this->efq->addMetaData('view', $view); + $this->query->buildFromEFQ($this->efq); + $expected_string = ' LIMIT ' . $view->query->pager->options['items_per_page']; + $this->assertNRQLContains($expected_string, 'EFQ-built query supports simple inferred range from Views "items_per_page" setting.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() range support via Views. + */ + function testBuildFromEFQViewsRangeTotalPages() { + // metaData['view']->query->pager->options['items_per_page']; + $view = (object) array( + 'query' => (object) array( + 'pager' => (object) array( + 'options' => array( + 'total_pages' => 75, + ), + ), + ), + ); + $this->efq->addMetaData('view', $view); + $this->query->buildFromEFQ($this->efq); + $expected_string = ' LIMIT ' . $view->query->pager->options['total_pages']; + $this->assertNRQLContains($expected_string, 'EFQ-built query supports simple inferred range from Views "total_pages" setting.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() range support via Views. + */ + function testBuildFromEFQViewsRangeTotalPagesPerPage() { + // metaData['view']->query->pager->options['items_per_page']; + $view = (object) array( + 'query' => (object) array( + 'pager' => (object) array( + 'options' => array( + 'items_per_page' => 3, + 'total_pages' => 75, + ), + ), + ), + ); + $this->efq->addMetaData('view', $view); + $this->query->buildFromEFQ($this->efq); + $expected_string = ' LIMIT ' . ($view->query->pager->options['total_pages'] * $view->query->pager->options['items_per_page']); + $this->assertNRQLContains($expected_string, 'EFQ-built query supports inferred range from Views "items_per_page" and "total_pages" settings.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() property condition support. + */ + function testBuildFromEFQPropertyCondition() { + $field = $this->randomName(); + $value = $this->randomName(); + $this->efq->propertyCondition($field, $value); + $this->query->buildFromEFQ($this->efq); + $this->assertNRQLContains("$field = '$value'", 'EFQ-built query supports property conditions.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() property condition support. + */ + function testBuildFromEFQEntityCondition() { + $field = $this->randomName(); + $value = $this->randomName(); + $this->efq->entityCondition($field, $value); + $this->query->buildFromEFQ($this->efq); + $this->assertNRQLContains("$field = '$value'", 'EFQ-built query supports entity conditions.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildFromEFQ() property condition support. + */ + function testBuildFromEFQFieldCondition() { + $field = $this->randomName(); + $value = $this->randomName(); + $this->efq->fields[0] = array('field' => $field, 'value' => $value); + $this->query->buildFromEFQ($this->efq); + $this->assertNRQLContains("$field = '$value'", 'EFQ-built query supports field conditions.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple bundle support. + */ + function testBuildNRQLMultiBundle() { + $bundles = array('Transaction', 'PageView'); + $this->query->entityCondition('bundle', $bundles, 'IN'); + + $expected_string = 'SELECT * FROM Transaction, PageView '; + $this->assertNRQLContains($expected_string, 'NRQL query multiple event types supported.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple condition support. + */ + function testBuildNRQLMultiConditions() { + $conditions = array( + array('field' => $this->randomName(), 'value' => $this->randomName()), + array('field' => $this->randomName(), 'value' => $this->randomName()), + ); + foreach ($conditions as $condition) { + $this->query->propertyCondition($condition['field'], $condition['value']); + } + + $expected_string = " WHERE {$conditions[0]['field']} = '{$conditions[0]['value']}' AND "; + $expected_string .= "{$conditions[1]['field']} = '{$conditions[1]['value']}' "; + $this->assertNRQLContains($expected_string, 'NRQL query multiple conditions supported.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple "IN" conditions. + */ + function testBuildNRQLMultiInConditions() { + $conditions = array( + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + ); + foreach ($conditions as $condition) { + $this->query->propertyCondition($condition['field'], $condition['value'], 'IN'); + } + + $expected_string = " WHERE ({$conditions[0]['field']} = '{$conditions[0]['value'][0]}' OR "; + $expected_string .= "{$conditions[0]['field']} = '{$conditions[0]['value'][1]}') AND "; + $expected_string .= " ({$conditions[1]['field']} = '{$conditions[1]['value'][0]}' OR "; + $expected_string .= "{$conditions[1]['field']} = '{$conditions[1]['value'][1]}') "; + // @todo Enable this once fixed in #2309343 + // $this->assertNRQLContains($expected_string, 'NRQL query multiple "IN" conditions supported.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple "IN" conditions that + * are preceded by a regular condition. + */ + function testBuildNRQLMultiInWithNormalConditions() { + $inConditions = array( + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + ); + $stdCondition = array('field' => $this->randomName(), 'value' => $this->randomName()); + + $this->query->propertyCondition($stdCondition['field'], $stdCondition['value']); + foreach ($inConditions as $condition) { + $this->query->propertyCondition($condition['field'], $condition['value'], 'IN'); + } + + $expected_string = " WHERE {$stdCondition['field']} = '{$stdCondition['value']}' AND ({$inConditions[0]['field']}"; + $this->assertNRQLContains($expected_string, 'NRQL query multiple "IN" conditions supported in combination with normal conditions.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple "OUT" conditions. + */ + function testBuildNRQLMultiOutConditions() { + $conditions = array( + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + ); + foreach ($conditions as $condition) { + $this->query->propertyCondition($condition['field'], $condition['value'], 'NOT IN'); + } + + $expected_string = " WHERE ({$conditions[0]['field']} != '{$conditions[0]['value'][0]}' AND "; + $expected_string .= "{$conditions[0]['field']} != '{$conditions[0]['value'][1]}' AND "; + $expected_string .= "{$conditions[1]['field']} != '{$conditions[1]['value'][0]}' AND "; + $expected_string .= "{$conditions[1]['field']} != '{$conditions[1]['value'][1]}') "; + $this->assertNRQLContains($expected_string, 'NRQL query multiple "NOT IN" conditions supported.'); + } + + /** + * Tests InsightRemoteEntityQuery::buildNRQL() multiple "OUT" conditions that + * are preceded by a regular condition. + */ + function testBuildNRQLMultiOutWithNormalConditions() { + $outConditions = array( + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + array('field' => $this->randomName(), 'value' => array($this->randomName(), $this->randomName())), + ); + $stdCondition = array('field' => $this->randomName(), 'value' => $this->randomName()); + + $this->query->propertyCondition($stdCondition['field'], $stdCondition['value']); + foreach ($outConditions as $condition) { + $this->query->propertyCondition($condition['field'], $condition['value'], 'NOT IN'); + } + + $expected_string = " WHERE {$stdCondition['field']} = '{$stdCondition['value']}' AND ({$outConditions[0]['field']}"; + $this->assertNRQLContains($expected_string, 'NRQL query multiple "NOT IN" conditions supported in combination with normal conditions.'); + } + }