Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from idimopoulos/entity-field-column
Browse files Browse the repository at this point in the history
ISAICP-6720: Handle field_name.entity case in entity query
  • Loading branch information
pfrenssen authored Jan 5, 2022
2 parents 58ad3e2 + f552ad2 commit 80e4ad3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
28 changes: 9 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,26 @@ mysql:

jobs:
include:
# PHP 7.3

- name: 'PHPCodeSniffer'
php: 7.3
php: 7.4
env: TEST_SUITE=PHPCodeSniffer

- name: 'PHPStan'
php: 7.3
env: TEST_SUITE=PHPStan DRUPAL=~8.9.0 PHPUNIT=^7

- name: 'PHPUnit with Drupal 8 on PHP 7.3'
php: 7.3
env: TEST_SUITE=PHPUnit DRUPAL=^8.9 PHPUNIT=^7

- name: 'PHPUnit with Drupal 9 on PHP 7.3'
php: 7.3
env: TEST_SUITE=PHPUnit DRUPAL=^9.1 PHPUNIT=^9

# PHP 7.4
- name: 'PHPUnit with Drupal 8 on PHP 7.4'
php: 7.4
env: TEST_SUITE=PHPUnit DRUPAL=^8.9 PHPUNIT=^7
env: TEST_SUITE=PHPStan DRUPAL=~8.9.20 PHPUNIT=^7

- name: 'PHPUnit with Drupal 9 on PHP 7.4'
php: 7.4
env: TEST_SUITE=PHPUnit DRUPAL=^9.1 PHPUNIT=^9
env: TEST_SUITE=PHPUnit DRUPAL=^9.2 PHPUNIT=^9

- name: 'PHPUnit with Drupal 9 on PHP 8.0'
php: 8.0
env: TEST_SUITE=PHPUnit DRUPAL=^9.2 PHPUNIT=^9

# PHP 8
- name: 'PHPUnit with Drupal 9 on PHP 8 nightly'
php: nightly
env: TEST_SUITE=PHPUnit DRUPAL=^9.1 PHPUNIT=^9
env: TEST_SUITE=PHPUnit DRUPAL=^9.2 PHPUNIT=^9

cache:
directories:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ml/json-ld": "^1.0"
},
"require-dev": {
"behat/mink": "~1.9.0",
"composer/installers": "^1.7",
"drupal/coder": "^8.3",
"drupal/core": "^8.9 || ^9.1",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
excludes_analyse:
excludePaths:
- ../testing_site/modules/sparql_entity_storage/vendor
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Query/Sparql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public function conditionAggregateGroupFactory($conjunction = 'AND') {
*/
protected function conditionGroupFactory($conjunction = 'AND') {
$class = static::getClass($this->namespaces, 'SparqlCondition');
return new $class($conjunction, $this, $this->namespaces, $this->graphHandler, $this->fieldHandler, $this->languageManager);
return new $class($conjunction, $this, $this->namespaces, $this->fieldHandler, $this->languageManager);
}

/**
Expand Down
30 changes: 14 additions & 16 deletions src/Entity/Query/Sparql/SparqlCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\sparql_entity_storage\SparqlEntityStorageFieldHandlerInterface;
use Drupal\sparql_entity_storage\SparqlEntityStorageGraphHandlerInterface;
use EasyRdf\Serialiser\Ntriples;

/**
* Defines the condition class for the entity query.
*/
class SparqlCondition extends ConditionFundamentals implements SparqlConditionInterface {

/**
* The SPARQL graph handler service.
*
* @var \Drupal\sparql_entity_storage\SparqlEntityStorageGraphHandlerInterface
*/
protected $graphHandler;

/**
* The SPARQL field mapping handler service.
*
Expand Down Expand Up @@ -152,7 +144,7 @@ class SparqlCondition extends ConditionFundamentals implements SparqlConditionIn
*
* @var array
*/
protected $fieldMappingConditions;
protected $fieldMappingConditions = [];

/**
* The entity type ID key.
Expand Down Expand Up @@ -194,25 +186,20 @@ class SparqlCondition extends ConditionFundamentals implements SparqlConditionIn
* The entity query this condition belongs to.
* @param array $namespaces
* List of potential namespaces of the classes belonging to this condition.
* @param \Drupal\sparql_entity_storage\SparqlEntityStorageGraphHandlerInterface $sparql_graph_handler
* The SPARQL graph handler service.
* @param \Drupal\sparql_entity_storage\SparqlEntityStorageFieldHandlerInterface $sparql_field_handler
* The SPARQL field mapping handler service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
*/
public function __construct($conjunction, SparqlQueryInterface $query, array $namespaces, SparqlEntityStorageGraphHandlerInterface $sparql_graph_handler, SparqlEntityStorageFieldHandlerInterface $sparql_field_handler, LanguageManagerInterface $language_manager) {
public function __construct($conjunction, SparqlQueryInterface $query, array $namespaces, SparqlEntityStorageFieldHandlerInterface $sparql_field_handler, LanguageManagerInterface $language_manager) {
$conjunction = strtoupper($conjunction);
parent::__construct($conjunction, $query, $namespaces);
$this->graphHandler = $sparql_graph_handler;
$this->fieldHandler = $sparql_field_handler;
$this->languageManager = $language_manager;
$this->typePredicate = $query->getEntityStorage()->getBundlePredicates();
$this->bundleKey = $query->getEntityType()->getKey('bundle');
$this->idKey = $query->getEntityType()->getKey('id');
$this->labelKey = $query->getEntityType()->getKey('label');
// Initialize variable to avoid warnings;.
$this->fieldMappingConditions = [];
$this->fieldMappings = [
$this->idKey => self::ID_KEY,
$this->bundleKey => count($this->typePredicate) === 1 ? reset($this->typePredicate) : SparqlArg::toVar($this->bundleKey . '_predicate'),
Expand Down Expand Up @@ -263,7 +250,18 @@ public function condition($field = NULL, $value = NULL, $operator = NULL, $lang
if (!$this->fieldHandler->fieldIsMapped($this->query->getEntityTypeId(), $field)) {
return $this;
}
$column = isset($field_name_parts[1]) ? $field_name_parts[1] : $this->fieldHandler->getFieldMainProperty($this->query->getEntityTypeId(), $field);

if (isset($field_name_parts[1])) {
// The 'entity' column designates an entity reference.
// @see \Drupal\Core\Entity\Query\QueryInterface::condition()
// @todo Handle more complex column representations, such as
// 'tags.entity.name' or tags.entity:taxonomy_term.name'.
$column = $field_name_parts[1] === 'entity' ? 'target_id' : $field_name_parts[1];
}
else {
$column = $this->fieldHandler->getFieldMainProperty($this->query->getEntityTypeId(), $field);
}

$this->conditions[] = [
'field' => $field,
'value' => $value,
Expand Down
14 changes: 13 additions & 1 deletion tests/src/Kernel/SparqlEntityQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ protected function setUp(): void {
}
}

/**
* Tests that the case field_ref.entity is handled.
*/
public function testEntityColumns(): void {
$ids = array_keys($this->getQuery()->condition('reference.entity', $this->entities[1]->id())->execute());
$this->assertSame([
'http://vegetable.example.com/004',
'http://vegetable.example.com/009',
], $ids);
}

/**
* Tests basic functionality related to Id and bundle filtering.
*/
Expand Down Expand Up @@ -635,7 +646,8 @@ protected function getTestEntityValues() {
protected function getQuery(string $operator = 'AND'): SparqlQueryInterface {
return $this->container->get('entity_type.manager')
->getStorage('sparql_test')
->getQuery($operator);
->getQuery($operator)
->accessCheck(FALSE);
}

}

0 comments on commit 80e4ad3

Please sign in to comment.