Skip to content

Bugfix/ticket-1209: Fixed single char query in PMQL (FOR 4.1) #4131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions ProcessMaker/Traits/ExtendedPMQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ProcessMaker\Query\Expression;
use ProcessMaker\Query\Traits\PMQL;
use Illuminate\Database\Eloquent\Builder;
use ProcessMaker\Query\IntervalExpression;
use Throwable;

trait ExtendedPMQL
Expand All @@ -24,7 +25,7 @@ trait ExtendedPMQL
* @param callable $callback
*
* @return mixed
*/
*/
public function scopePMQL(Builder $builder, string $query, callable $callback = null)
{
if (! $callback) {
Expand All @@ -46,22 +47,22 @@ public function scopePMQL(Builder $builder, string $query, callable $callback =
* @param \Illuminate\Database\Eloquent\Builder $builder
*
* @return mixed
*/
*/
private function handle(Expression $expression, Builder $builder)
{
// Setup our needed variables
$field = $expression->field->field();
$model = $builder->getModel();

if (is_string($field)) {
// Parse our value
$value = $this->parseValue($expression);

// PMQL Interval expressions do not have values
if (method_exists($expression->value, 'setValue')) {
$expression->value->setValue($value);
}

// Title case our field name so we can suffix it to our method names
$fieldMethodName = ucfirst(strtolower($field));

Expand All @@ -86,10 +87,10 @@ private function handle(Expression $expression, Builder $builder)
$method = "fieldWildcard";
if (method_exists($model, $method)) {
return $model->{$method}($value, $expression, $builder);
}
}
}
}

/**
* Set the value as a string if possible. Also convert to the logged-in
* user's timezone if the value is parsable by Carbon as a date.
Expand All @@ -106,9 +107,9 @@ private function parseValue($expression)
} else {
$value = $expression->value;
}

// Check to see if the value is parsable as a date
if (! is_numeric($value)) {
if ($value instanceof IntervalExpression || (is_string($value) && strlen($value) > 1)) {
try {
$parsed = Carbon::parse($value, auth()->user()->timezone);
$parsed->setTimezone(config('app.timezone'));
Expand All @@ -117,7 +118,7 @@ private function parseValue($expression)
//Ignore parsing errors and just return the original
}
}

return $value;
}
}
42 changes: 29 additions & 13 deletions tests/Feature/ExtendedPMQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ public function testHandleFieldAlias()
{
// Instantiate Faker
$faker = Factory::create();

// Set our fake date
$date = $faker->dateTime();

// Create a process request with our fake created_at date
$processRequest = factory(ProcessRequest::class)->create([
'created_at' => $date,
]);

// Construct & run a PMQL query using the "created" field alias
$query = 'created = "' . $date->format('Y-m-d H:i:s') . '"';
$pmqlResult = ProcessRequest::pmql($query)->first();

// Assert that the models match
$this->assertTrue($processRequest->is($pmqlResult));
}

public function testHandleValueAlias()
{
// Create a process request
$processRequest = factory(ProcessRequest::class)->create([
'status' => 'ACTIVE',
]);

// Construct & run a PMQL query using the "status" value alias
$query = 'status = "In Progress"';
$pmqlResult = ProcessRequest::pmql($query)->get();

// Assert that the model is returned in our search
$ids = $pmqlResult->pluck('id');
$this->assertContains($processRequest->id, $ids);
}

public function testHandleFieldWildcard()
{
$this->markTestSkipped('PMQL does not yet support JSON fields on Microsoft SQL Server.');

// Instantiate Faker
$faker = Factory::create();

Expand All @@ -68,18 +68,18 @@ public function testHandleFieldWildcard()
$processRequest = factory(ProcessRequest::class)->create([
'data' => $data,
]);

// Create a process request token tied to the above request
$processRequestToken = factory(ProcessRequestToken::class)->create([
'process_request_id' => $processRequest->id,
]);

// Construct & run a PMQL query using the "data" field wildcard
$query = "data.first_name = \"{$data['first_name']}\" AND data.last_name = \"{$data['last_name']}\"";
$pmqlResult = ProcessRequestToken::pmql($query)->first();

// Assert that the models match
$this->assertTrue($processRequestToken->is($pmqlResult));
$this->assertTrue($processRequestToken->is($pmqlResult));
}

public function testInUsersTimezone()
Expand Down Expand Up @@ -118,7 +118,23 @@ public function testRelativeDate()

$url = route('api.requests.index', ['pmql' => 'data.date > now -1 hour']);
$result = $this->apiCall('GET', $url);
$this->assertCount(1, $result->json()['data']); // Match only the one that completed 10 minutes ago
$this->assertCount(1, $result->json()['data']); // Match only the one that completed 10 minutes ago
$this->assertEquals($processRequest1->id, $result->json()['data'][0]['id'] );
}

public function testCharComparison()
{
$processRequest1 = factory(ProcessRequest::class)->create([
'data' => ['gender' => 'F']
]);

$processRequest2 = factory(ProcessRequest::class)->create([
'data' => ['gender' => 'M']
]);

$url = route('api.requests.index', ['pmql' => 'data.gender = "F"']);
$result = $this->apiCall('GET', $url);
$this->assertCount(1, $result->json()['data']); // Match only F
$this->assertEquals($processRequest1->id, $result->json()['data'][0]['id'] );
}
}