Skip to content

Commit be817ad

Browse files
committed
Merge branch 'main' into 496
# Conflicts: # tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php
2 parents fedf529 + aea0372 commit be817ad

7 files changed

+278
-6
lines changed

src/Type/DrupalStaticEntityQueryDynamicReturnTypeExtension.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ public function getTypeFromStaticMethodCall(
5757
if ($type instanceof ConstantStringType) {
5858
$entityTypeId = $type->getValue();
5959
} else {
60-
// @todo determine what these types are, and try to resolve entity name from.
61-
return $returnType;
60+
// We're unsure what specific EntityQueryType it is, so let's stick
61+
// with the general class itself to ensure it gets access checked.
62+
return new EntityQueryType(
63+
$returnType->getClassName(),
64+
$returnType->getSubtractedType(),
65+
$returnType->getClassReflection()
66+
);
6267
}
6368
$entityType = $this->entityDataRepository->get($entityTypeId);
6469
$entityStorageType = $entityType->getStorageType();

src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function getTypeFromMethodCall(
7272
? new ArrayType(new IntegerType(), new StringType())
7373
: new EntityQueryExecuteWithoutAccessCheckType(new IntegerType(), new StringType());
7474
}
75+
if ($varType instanceof EntityQueryType) {
76+
return $varType->hasAccessCheck()
77+
? new ArrayType(new IntegerType(), new StringType())
78+
: new EntityQueryExecuteWithoutAccessCheckType(new IntegerType(), new StringType());
79+
}
7580
return $defaultReturnType;
7681
}
7782

src/Type/EntityStorage/GetQueryReturnTypeExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public function getClass(): string
2525

2626
public function isMethodSupported(MethodReflection $methodReflection): bool
2727
{
28-
return $methodReflection->getName() === 'getQuery';
28+
return in_array($methodReflection->getName(), [
29+
'getQuery',
30+
'getAggregateQuery',
31+
], true);
2932
}
3033

3134
public function getTypeFromMethodCall(

tests/src/Rules/EntityParameterTypehintRuleTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ final class EntityParameterTypehintRuleTest extends DrupalRuleTestCase
1111
{
1212
protected function getRule(): Rule
1313
{
14-
$broker = $this->createReflectionProvider();
1514
// @phpstan-ignore-next-line
16-
return new MissingFunctionParameterTypehintRule(new MissingTypehintCheck($broker, true, true, true, true, []));
15+
return new MissingFunctionParameterTypehintRule(
16+
// @phpstan-ignore-next-line
17+
new MissingTypehintCheck(
18+
true,
19+
true,
20+
true,
21+
true,
22+
[]
23+
));
1724
}
1825

1926
public function testRule(): void

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,21 @@ public function cases(): \Generator
8686
[__DIR__ . '/data/bug-437.php'],
8787
[]
8888
];*/
89-
89+
yield 'bug-474.php' => [
90+
[__DIR__ . '/data/bug-474.php'],
91+
[
92+
[
93+
'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.',
94+
49,
95+
'See https://www.drupal.org/node/3201242',
96+
],
97+
[
98+
'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.',
99+
98,
100+
'See https://www.drupal.org/node/3201242',
101+
],
102+
]
103+
];
90104
yield 'bug-475.php' => [
91105
[__DIR__.'/data/bug-475.php'],
92106
[]
@@ -97,6 +111,27 @@ public function cases(): \Generator
97111
[]
98112
];
99113

114+
yield 'bug-494.php' => [
115+
[__DIR__.'/data/bug-494.php'],
116+
[
117+
[
118+
'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.',
119+
43,
120+
'See https://www.drupal.org/node/3201242',
121+
],
122+
[
123+
'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.',
124+
103,
125+
'See https://www.drupal.org/node/3201242',
126+
],
127+
[
128+
'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.',
129+
108,
130+
'See https://www.drupal.org/node/3201242',
131+
],
132+
]
133+
];
134+
100135
yield 'bug-496.php' => [
101136
[__DIR__.'/data/bug-496.php'],
102137
[]

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Bug474;
4+
5+
class TestClass {
6+
7+
/**
8+
* Tests entity queries using getAggregateQuery with access check.
9+
*/
10+
function bug474(): void
11+
{
12+
\Drupal::entityTypeManager()->getStorage('node')
13+
->getAggregateQuery()
14+
->accessCheck(FALSE)
15+
->condition('field_test', 'foo', '=')
16+
->execute();
17+
18+
\Drupal::entityTypeManager()->getStorage('node')
19+
->getAggregateQuery()
20+
->accessCheck(FALSE)
21+
->condition('field_test', 'foo', '=')
22+
->execute();
23+
24+
\Drupal::entityTypeManager()->getStorage('node')
25+
->getAggregateQuery()
26+
->condition('field_test', 'foo', '=')
27+
->accessCheck(TRUE)
28+
->execute();
29+
30+
\Drupal::entityTypeManager()->getStorage('node')
31+
->getAggregateQuery()
32+
->accessCheck(TRUE)
33+
->condition('field_test', 'foo', '=')
34+
->execute();
35+
36+
\Drupal::entityTypeManager()->getStorage('node')
37+
->getAggregateQuery()
38+
->condition('field_test', 'foo', '=')
39+
->accessCheck()
40+
->execute();
41+
42+
\Drupal::entityTypeManager()->getStorage('node')
43+
->getAggregateQuery()
44+
->accessCheck()
45+
->condition('field_test', 'foo', '=')
46+
->execute();
47+
48+
// Failing test due to missing accessCheck.
49+
\Drupal::entityTypeManager()->getStorage('node')
50+
->getAggregateQuery()
51+
->condition('field_test', 'foo', '=')
52+
->execute();
53+
54+
// Same tests but now with count() chained as well.
55+
\Drupal::entityTypeManager()->getStorage('node')
56+
->getAggregateQuery()
57+
->accessCheck(FALSE)
58+
->condition('field_test', 'foo', '=')
59+
->count()
60+
->execute();
61+
62+
\Drupal::entityTypeManager()->getStorage('node')
63+
->getAggregateQuery()
64+
->count()
65+
->accessCheck(FALSE)
66+
->condition('field_test', 'foo', '=')
67+
->execute();
68+
69+
\Drupal::entityTypeManager()->getStorage('node')
70+
->getAggregateQuery()
71+
->condition('field_test', 'foo', '=')
72+
->accessCheck(TRUE)
73+
->count()
74+
->execute();
75+
76+
\Drupal::entityTypeManager()->getStorage('node')
77+
->getAggregateQuery()
78+
->count()
79+
->accessCheck(TRUE)
80+
->condition('field_test', 'foo', '=')
81+
->execute();
82+
83+
\Drupal::entityTypeManager()->getStorage('node')
84+
->getAggregateQuery()
85+
->condition('field_test', 'foo', '=')
86+
->accessCheck()
87+
->count()
88+
->execute();
89+
90+
\Drupal::entityTypeManager()->getStorage('node')
91+
->getAggregateQuery()
92+
->count()
93+
->accessCheck()
94+
->condition('field_test', 'foo', '=')
95+
->execute();
96+
97+
// Failing test due to missing accessCheck.
98+
\Drupal::entityTypeManager()->getStorage('node')
99+
->getAggregateQuery()
100+
->condition('field_test', 'foo', '=')
101+
->count()
102+
->execute();
103+
}
104+
}

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Bug494Example;
4+
5+
class TestClass {
6+
7+
/**
8+
* Tests entity queries with access check and unknown entity type.
9+
*/
10+
public function bug494(string $entity_type): void
11+
{
12+
\Drupal::entityQuery($entity_type)
13+
->accessCheck(FALSE)
14+
->condition('field_test', 'foo', '=')
15+
->execute();
16+
17+
\Drupal::entityQuery($entity_type)
18+
->condition('field_test', 'foo', '=')
19+
->accessCheck(FALSE)
20+
->execute();
21+
22+
\Drupal::entityQuery($entity_type)
23+
->accessCheck(TRUE)
24+
->condition('field_test', 'foo', '=')
25+
->execute();
26+
27+
\Drupal::entityQuery($entity_type)
28+
->condition('field_test', 'foo', '=')
29+
->accessCheck(TRUE)
30+
->execute();
31+
32+
\Drupal::entityQuery($entity_type)
33+
->accessCheck()
34+
->condition('field_test', 'foo', '=')
35+
->execute();
36+
37+
\Drupal::entityQuery($entity_type)
38+
->condition('field_test', 'foo', '=')
39+
->accessCheck()
40+
->execute();
41+
42+
// Failing test due to missing accessCheck.
43+
\Drupal::entityQuery($entity_type)
44+
->condition('field_test', 'foo', '=')
45+
->execute();
46+
47+
// Same tests but now with count() chained as well.
48+
\Drupal::entityQuery($entity_type)
49+
->accessCheck(FALSE)
50+
->condition('field_test', 'foo', '=')
51+
->count()
52+
->execute();
53+
54+
\Drupal::entityQuery($entity_type)
55+
->condition('field_test', 'foo', '=')
56+
->accessCheck(FALSE)
57+
->count()
58+
->execute();
59+
60+
\Drupal::entityQuery($entity_type)
61+
->condition('field_test', 'foo', '=')
62+
->count()
63+
->accessCheck(FALSE)
64+
->execute();
65+
66+
\Drupal::entityQuery($entity_type)
67+
->accessCheck(TRUE)
68+
->condition('field_test', 'foo', '=')
69+
->count()
70+
->execute();
71+
72+
\Drupal::entityQuery($entity_type)
73+
->condition('field_test', 'foo', '=')
74+
->accessCheck(TRUE)
75+
->count()
76+
->execute();
77+
78+
\Drupal::entityQuery($entity_type)
79+
->condition('field_test', 'foo', '=')
80+
->count()
81+
->accessCheck(TRUE)
82+
->execute();
83+
84+
\Drupal::entityQuery($entity_type)
85+
->accessCheck()
86+
->condition('field_test', 'foo', '=')
87+
->count()
88+
->execute();
89+
90+
\Drupal::entityQuery($entity_type)
91+
->condition('field_test', 'foo', '=')
92+
->accessCheck()
93+
->count()
94+
->execute();
95+
96+
\Drupal::entityQuery($entity_type)
97+
->condition('field_test', 'foo', '=')
98+
->count()
99+
->accessCheck()
100+
->execute();
101+
102+
// Failing tests due to missing accessCheck.
103+
\Drupal::entityQuery($entity_type)
104+
->condition('field_test', 'foo', '=')
105+
->count()
106+
->execute();
107+
108+
\Drupal::entityQuery($entity_type)
109+
->count()
110+
->condition('field_test', 'foo', '=')
111+
->execute();
112+
}
113+
}

0 commit comments

Comments
 (0)