Skip to content

Commit cc5b129

Browse files
authored
Merge pull request #495 from Boegie/494
Fixes #494
2 parents 5e8b527 + dbaaea7 commit cc5b129

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
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

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,26 @@ public function cases(): \Generator
9696
[__DIR__.'/data/bug-475b.php'],
9797
[]
9898
];
99+
100+
yield 'bug-494.php' => [
101+
[__DIR__.'/data/bug-494.php'],
102+
[
103+
[
104+
'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.',
105+
43,
106+
'See https://www.drupal.org/node/3201242',
107+
],
108+
[
109+
'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.',
110+
103,
111+
'See https://www.drupal.org/node/3201242',
112+
],
113+
[
114+
'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.',
115+
108,
116+
'See https://www.drupal.org/node/3201242',
117+
],
118+
]
119+
];
99120
}
100121
}

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)