Skip to content

Commit 8f3abe6

Browse files
committed
Add test for query with access as property
issue #437
1 parent 165497a commit 8f3abe6

File tree

7 files changed

+126
-81
lines changed

7 files changed

+126
-81
lines changed

src/Type/DrupalStaticEntityQueryDynamicReturnTypeExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
66
use mglaman\PHPStanDrupal\Type\EntityQuery\ConfigEntityQueryType;
77
use mglaman\PHPStanDrupal\Type\EntityQuery\ContentEntityQueryType;
8+
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryType;
89
use mglaman\PHPStanDrupal\Type\EntityStorage\ConfigEntityStorageType;
910
use mglaman\PHPStanDrupal\Type\EntityStorage\ContentEntityStorageType;
1011
use PhpParser\Node\Expr\StaticCall;
@@ -80,6 +81,10 @@ public function getTypeFromStaticMethodCall(
8081
);
8182
}
8283

83-
return $returnType;
84+
return new EntityQueryType(
85+
$returnType->getClassName(),
86+
$returnType->getSubtractedType(),
87+
$returnType->getClassReflection()
88+
);
8489
}
8590
}

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function cases(): \Generator
4949
[__DIR__ . '/data/bug-438.php'],
5050
[]
5151
];
52-
yield 'bug-396a.php' => [
53-
[__DIR__.'/data/bug-396a.php'],
52+
yield 'bug-396a1.php' => [
53+
[__DIR__.'/data/bug-396a1.php'],
5454
[
5555
[
5656
'Missing explicit access check on entity query.',
@@ -59,6 +59,14 @@ public function cases(): \Generator
5959
]
6060
]
6161
];
62+
yield 'bug-396a2.php' => [
63+
[__DIR__ . '/data/bug-396a2.php'],
64+
[]
65+
];
66+
yield 'bug-396a3.php' => [
67+
[__DIR__ . '/data/bug-396a3.php'],
68+
[]
69+
];
6270
yield 'bug-396b.php' => [
6371
[__DIR__ . '/data/bug-396b.php'],
6472
[]
@@ -67,5 +75,9 @@ public function cases(): \Generator
6775
[__DIR__ . '/data/bug-396c.php'],
6876
[]
6977
];
78+
yield 'bug-437.php' => [
79+
[__DIR__ . '/data/bug-437.php'],
80+
[]
81+
];
7082
}
7183
}

tests/src/Rules/data/bug-396a.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/src/Rules/data/bug-396a1.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug396a;
4+
5+
function foo() {
6+
$query = \Drupal::entityQuery('user')
7+
->condition('status', 1)
8+
->condition('uid', 1, '<>')
9+
->condition('field_profile_visibility', 1)
10+
->condition('field_account_type', '', '<>')
11+
->condition('field_last_name.value', '', '<>');
12+
if( isset($qparam['expertise']) && !empty($qparam['expertise']) ) {
13+
$query->condition('field_field_expertise.entity.tid', $qparam['expertise']);
14+
}
15+
if( isset($qparam['origin']) && !empty($qparam['origin']) ) {
16+
$query->condition('field_place_origin.entity:taxonomy_term.field_country_tags', $qparam['origin']);
17+
}
18+
if( isset($qparam['place_residence']) && !empty($qparam['place_residence']) ) {
19+
$query->condition('field_place_residence.entity:taxonomy_term.field_country_tags', $qparam['place_residence']);
20+
}
21+
if( isset($qparam['city']) && !empty($qparam['city']) ) {
22+
$query->condition('field_city', $qparam['city'], 'CONTAINS');
23+
}
24+
if( isset($qparam['user_type']) && !empty($qparam['user_type']) ) {
25+
$query->condition('field_account_type.entity.tid', $qparam['user_type']);
26+
}
27+
$users = $query->execute();
28+
};

tests/src/Rules/data/bug-396a2.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug396a;
4+
5+
function bar() {
6+
$query = \Drupal::entityQuery('user')
7+
->accessCheck(FALSE)
8+
->condition('status', 1)
9+
->condition('uid', 1, '<>')
10+
->condition('field_profile_visibility', 1)
11+
->condition('field_account_type', '', '<>')
12+
->condition('field_last_name.value', '', '<>');
13+
if( isset($qparam['expertise']) && !empty($qparam['expertise']) ) {
14+
$query->condition('field_field_expertise.entity.tid', $qparam['expertise']);
15+
}
16+
if( isset($qparam['origin']) && !empty($qparam['origin']) ) {
17+
$query->condition('field_place_origin.entity:taxonomy_term.field_country_tags', $qparam['origin']);
18+
}
19+
if( isset($qparam['place_residence']) && !empty($qparam['place_residence']) ) {
20+
$query->condition('field_place_residence.entity:taxonomy_term.field_country_tags', $qparam['place_residence']);
21+
}
22+
if( isset($qparam['city']) && !empty($qparam['city']) ) {
23+
$query->condition('field_city', $qparam['city'], 'CONTAINS');
24+
}
25+
if( isset($qparam['user_type']) && !empty($qparam['user_type']) ) {
26+
$query->condition('field_account_type.entity.tid', $qparam['user_type']);
27+
}
28+
$users = $query->execute();
29+
};

tests/src/Rules/data/bug-396a3.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug396a;
4+
5+
function baz() {
6+
$query = \Drupal::entityQuery('user')
7+
->condition('status', 1)
8+
->condition('uid', 1, '<>')
9+
->condition('field_profile_visibility', 1)
10+
->condition('field_account_type', '', '<>')
11+
->condition('field_last_name.value', '', '<>');
12+
if( isset($qparam['expertise']) && !empty($qparam['expertise']) ) {
13+
$query->condition('field_field_expertise.entity.tid', $qparam['expertise']);
14+
}
15+
if( isset($qparam['origin']) && !empty($qparam['origin']) ) {
16+
$query->condition('field_place_origin.entity:taxonomy_term.field_country_tags', $qparam['origin']);
17+
}
18+
if( isset($qparam['place_residence']) && !empty($qparam['place_residence']) ) {
19+
$query->condition('field_place_residence.entity:taxonomy_term.field_country_tags', $qparam['place_residence']);
20+
}
21+
if( isset($qparam['city']) && !empty($qparam['city']) ) {
22+
$query->condition('field_city', $qparam['city'], 'CONTAINS');
23+
}
24+
if( isset($qparam['user_type']) && !empty($qparam['user_type']) ) {
25+
$query->condition('field_account_type.entity.tid', $qparam['user_type']);
26+
}
27+
$query->accessCheck(FALSE);
28+
$users = $query->execute();
29+
};

tests/src/Rules/data/bug-437.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug437;
4+
5+
use Drupal\Core\Entity\EntityTypeManagerInterface;
6+
use Drupal\Core\Entity\Query\QueryInterface;
7+
8+
final class Foo {
9+
private QueryInterface $vocabularyQuery;
10+
public function __construct(EntityTypeManagerInterface $entityTypeManager)
11+
{
12+
$this->vocabularyQuery = $entityTypeManager
13+
->getStorage('taxonomy_term')
14+
->getQuery()
15+
->accessCheck(TRUE);
16+
}
17+
public function a(): int {
18+
return $this->vocabularyQuery->count()->execute();
19+
}
20+
}

0 commit comments

Comments
 (0)