Skip to content
Snippets Groups Projects
Commit 2409bd5d authored by Josh Pollock's avatar Josh Pollock
Browse files

impliment select by field value feature

parent 7f4cef58
No related branches found
No related tags found
No related merge requests found
......@@ -3,15 +3,21 @@ namespace calderawp\CalderaFormsQuery\Tests\Integration\Features;
use calderawp\CalderaFormsQuery\CreatesSelectQueries;
use calderawp\CalderaFormsQuery\Features\FeatureContainer;
use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase;
use calderawp\CalderaFormsQuery\Tests\Traits\CanCreateEntryWithEmailField;
class QueryByUserIdTest extends IntegrationTestCase
class FeatureHelperMethodsTest extends IntegrationTestCase
{
use CanCreateEntryWithEmailField;
/**
*
* @covers FeatureContainer::selectByUserId()
* @covers FeatureContainer::collectResults()
* @covers FeatureContainer::collectEntryValues()
*/
public function testByUserId()
{
$container = $this->containerFactory();
......@@ -41,4 +47,34 @@ class QueryByUserIdTest extends IntegrationTestCase
}
/**
* Test selecting by a field value such as an email
*
* @covers FeatureContainer::selectByFieldValue()
*/
public function testByFieldValue()
{
$container = $this->containerFactory();
//Create one entry for unknown user
$this->createEntryWithEmail( rand(). 'email.com' );
//Create two entries for a known user.
$email = 'nom@noms.noms';
$userId = $this->factory()->user->create(
[ 'user_email' => $email ]
);
wp_set_current_user( $userId );
$this->createEntryWithEmail( $email );
$this->createEntryWithEmail( $email );
$results = $container->selectByFieldValue(
$this->getEmailFieldSlug(),
$email
);
$this->assertSame(2, count($results));
$this->assertSame( $email,$results[0]['values'][1]->value );
$this->assertSame( $email,$results[1]['values'][1]->value );
}
}
\ No newline at end of file
......@@ -98,7 +98,14 @@ abstract class IntegrationTestCase extends \WP_UnitTestCase
return $this->create_entry( $this->mock_form );
}
/**
* @return int
*/
protected function createEntryWithMockFormAndGetEntryId()
{
$details = $this->create_entry( $this->mock_form );
return $details[ 'id' ];
}
......
......@@ -57,7 +57,7 @@ class EntryTest extends IntegrationTestCase
*/
public function testQueryByEntryId()
{
$entry = $this->createEntryWithMockForm();
$entryId = $this->createEntryWithMockFormAndGetEntryId();
$entryGenerator = $this->entryGeneratorFactory();
//No results for a non-existent entry
$entryGenerator->queryByEntryId(42);
......@@ -66,24 +66,57 @@ class EntryTest extends IntegrationTestCase
//One entry: one result with the correct ID
$entryGenerator = $this->entryGeneratorFactory();
$entryGenerator->queryByEntryId($entry['id']);
$entryGenerator->queryByEntryId($entryId);
$sql = $entryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $results ));
$this->assertSame( 1, count( $results ) );
$this->assertEquals( $results[0]->id, $entry['id']);
$this->assertEquals( $results[0]->id, $entryId);
//Two more entries: one result for original entry ID
$this->createEntryWithMockForm();
$this->createEntryWithMockForm();
$entryGenerator = $this->entryGeneratorFactory();
$entryGenerator->queryByEntryId($entry['id']);
$entryGenerator->queryByEntryId($entryId);
$sql = $entryGenerator->getPreparedSql();
$results = $this->queryWithWPDB($sql);
$this->assertTrue( ! empty( $results ));
$this->assertSame( 1, count( $results ) );
}
/**
* Test querying by IDs
*
* @covers Entry::queryByEntryIds()
*/
public function testByEntryIds()
{
$entryIdOne = $this->createEntryWithMockFormAndGetEntryId();
$entryIdTwo = $this->createEntryWithMockFormAndGetEntryId();
$entryIdThree = $this->createEntryWithMockFormAndGetEntryId();
//Two results when asking for IN One and Three
$entryGenerator = $this->entryGeneratorFactory();
$sql = $entryGenerator->queryByEntryIds( [
$entryIdOne,
$entryIdThree
])
->getPreparedSql();
$results = $this->queryWithWPDB( $sql );
$this->assertSame( 2, count( $results ) );
//One results when asking for IN Two
$entryGenerator = $this->entryGeneratorFactory();
$sql = $entryGenerator->queryByEntryIds( [
$entryIdTwo
])
->getPreparedSql();
$results = $this->queryWithWPDB( $sql );
$this->assertSame( 1, count( $results ) );
}
/**
* Test query by user ID
*
......@@ -140,4 +173,6 @@ class EntryTest extends IntegrationTestCase
}
\ No newline at end of file
......@@ -134,6 +134,41 @@ class FeatureContainer extends Container
return $this->collectResults( $this->select( $query ) );
}
/**
* Find all entries that have or do not have field with a slug and value
*
* @param string $fieldSlug Field slug
* @param string $fieldValue Field value
* @param bool $have Optional. Default: true. If true query is for fields with this value
*
* @return array
*/
public function selectByFieldValue($fieldSlug, $fieldValue, $have = true )
{
$type = $have ? 'equals' : 'notEquals';
$queryForEntryValues = $this
->getQueries()
->entryValuesSelect()
->queryByFieldValue($fieldSlug, $fieldValue, $type, 'AND', [
'entry_id'
] );
$results = $this->select( $queryForEntryValues );
if( empty( $results ) || 0 >= count( $results )){
return [];
}
foreach ( $results as &$result ){
$result = $result->entry_id;
}
$queryForValues = $this
->getQueries()
->entrySelect()
->queryByEntryIds($results);
return $this->collectResults( $this->select( $queryForValues ) );
}
/**
* @return string
*/
......
......@@ -33,6 +33,22 @@ class Entry extends SelectQueryBuilder
return $this->is('id', $entryId);
}
/**
* Query by entry ids
*
* @param array $entryIds An array of IDs to query by
*
* @return $this
*/
public function queryByEntryIds(array $entryIds)
{
$this
->getSelectQuery()
->where()
->in( 'id', $entryIds );
return $this;
}
/**
* Get all entries for a specific user
*
......
......@@ -30,10 +30,16 @@ class EntryValues extends SelectQueryBuilder
* @param string $fieldValue Field value
* @param string $type Optional. Type of comparison. Values: equals|notEquals|like Default: 'equals'
* @param string $whereOperator Optional. Type of where. Default is 'AND'. Any valid WHERE operator is accepted
* @param array $columns Optional. Array of columns to select. Leave empty to select *
* @return $this
*/
public function queryByFieldValue($fieldSlug, $fieldValue, $type = 'equals', $whereOperator = 'AND')
public function queryByFieldValue($fieldSlug, $fieldValue, $type = 'equals', $whereOperator = 'AND', $columns = [])
{
if( ! empty( $columns ) ){
$this
->getSelectQuery()
->setColumns( $columns );
}
switch ($type) {
case 'equals':
$this
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment