Skip to content

Commit 4ce5426

Browse files
committed
Test fixes
1 parent a05e7bd commit 4ce5426

File tree

17 files changed

+84
-60
lines changed

17 files changed

+84
-60
lines changed

src/Database/Queries/Concerns/QueriesEntryTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function initQueriesEntryTypes(): void
5353
return;
5454
}
5555

56-
$entryQuery->subQuery->whereIn('entries.typeId', $this->typeId);
56+
$entryQuery->subQuery->whereIn('entries.typeId', $entryQuery->typeId);
5757
});
5858
}
5959

src/Database/Queries/ElementQuery.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use craft\elements\ElementCollection;
1212
use craft\helpers\ElementHelper;
1313
use CraftCms\Cms\Database\Queries\Exceptions\ElementNotFoundException;
14+
use CraftCms\Cms\Database\Queries\Exceptions\QueryAbortedException;
1415
use CraftCms\Cms\Database\Table;
1516
use CraftCms\Cms\Support\Arr;
1617
use CraftCms\Cms\Support\Typecast;
@@ -31,6 +32,7 @@
3132
use ReflectionException;
3233
use ReflectionMethod;
3334
use ReflectionProperty;
35+
use RuntimeException;
3436
use Tpetry\QueryExpressions\Function\Conditional\Coalesce;
3537
use Tpetry\QueryExpressions\Language\Alias;
3638
use Twig\Markup;
@@ -448,7 +450,11 @@ public function getModels(array|string $columns = ['*']): array
448450
return $result;
449451
}
450452

451-
$this->applyBeforeQueryCallbacks();
453+
try {
454+
$this->applyBeforeQueryCallbacks();
455+
} catch (QueryAbortedException) {
456+
return [];
457+
}
452458

453459
if ((int) $this->queryCacheDuration >= 0) {
454460
$result = DependencyCache::remember(
@@ -499,7 +505,11 @@ public function pluck($column, $key = null): Collection|array
499505
return collect($result)->pluck($column, $key);
500506
}
501507

502-
$this->applyBeforeQueryCallbacks();
508+
try {
509+
$this->applyBeforeQueryCallbacks();
510+
} catch (QueryAbortedException) {
511+
return $this->asArray ? [] : new Collection;
512+
}
503513

504514
$column = $this->columnMap[$column] ?? $column;
505515

@@ -523,7 +533,11 @@ public function count($columns = '*'): int
523533
return count($result);
524534
}
525535

526-
$this->applyBeforeQueryCallbacks();
536+
try {
537+
$this->applyBeforeQueryCallbacks();
538+
} catch (QueryAbortedException) {
539+
return 0;
540+
}
527541

528542
$eagerLoadedCount = $this->eagerLoad(count: true);
529543

@@ -593,7 +607,11 @@ public function applyAfterQueryCallbacks(mixed $result): mixed
593607
*/
594608
public function cursor(): LazyCollection
595609
{
596-
$this->applyBeforeQueryCallbacks();
610+
try {
611+
$this->applyBeforeQueryCallbacks();
612+
} catch (QueryAbortedException) {
613+
return new LazyCollection;
614+
}
597615

598616
return $this->query->cursor()->map(function ($record) {
599617
$model = $this->createElement((array) $record);
@@ -799,7 +817,11 @@ public function __call($method, $parameters): mixed
799817
}
800818

801819
if (in_array(strtolower($method), $this->passthru)) {
802-
$this->applyBeforeQueryCallbacks();
820+
try {
821+
$this->applyBeforeQueryCallbacks();
822+
} catch (QueryAbortedException) {
823+
return null;
824+
}
803825

804826
if ((int) $this->queryCacheDuration >= 0) {
805827
return DependencyCache::remember(
@@ -928,11 +950,6 @@ public function applyBeforeQueryCallbacks(): void
928950

929951
protected function elementQueryBeforeQuery(): void
930952
{
931-
// Give other classes a chance to make changes up front
932-
/*if (!$this->beforePrepare()) {
933-
throw new QueryAbortedException();
934-
}*/
935-
936953
$this->applySelectParams();
937954

938955
// If an element table was never joined in, explicitly filter based on the element type
@@ -1071,4 +1088,18 @@ private function resolveColumnMapping(string $key): string|array
10711088

10721089
return $this->columnMap[$key];
10731090
}
1091+
1092+
/**
1093+
* Throw an exception if the query doesn't have an orderBy clause.
1094+
*
1095+
* @return void
1096+
*
1097+
* @throws \RuntimeException
1098+
*/
1099+
protected function enforceOrderBy()
1100+
{
1101+
if (empty($this->query->orders) && empty($this->query->unionOrders)) {
1102+
throw new RuntimeException('You must specify an orderBy clause when using this function.');
1103+
}
1104+
}
10741105
}

src/Database/Queries/Exceptions/QueryAbortedException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44

55
namespace CraftCms\Cms\Database\Queries\Exceptions;
66

7-
use Exception;
8-
9-
final class QueryAbortedException extends Exception {}
7+
/** @TODO Replace legacy aborted with this one */
8+
final class QueryAbortedException extends \craft\db\QueryAbortedException {}

src/Section/Sections.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ private function ensureSingleEntry(Section $section, ?array $siteSettings = null
835835
// If there are any existing entries, find the first one with a valid typeId
836836
/** @var Entry|null $entry */
837837
$entry = $baseEntryQuery
838+
->clone()
838839
->typeId($entryTypeIds)
839840
->first();
840841

@@ -843,6 +844,7 @@ private function ensureSingleEntry(Section $section, ?array $siteSettings = null
843844
if ($entry === null) {
844845
/** @var Entry|null $entry */
845846
$entry = $baseEntryQuery
847+
->clone()
846848
->typeId(null)
847849
->trashed(null)
848850
->first();

tests/Database/Queries/Concerns/CachesQueriesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
expect(entryQuery()->count())->toBe(2);
2020
expect(entryQuery()->all()->count())->toBe(2);
2121

22-
Craft::$app->getElements()->invalidateCachesForElementType(\craft\elements\Entry::class);
22+
Craft::$app->getElements()->invalidateCachesForElementType(\CraftCms\Cms\Element\Elements\Entry::class);
2323

2424
expect(entryQuery()->cache()->count())->toBe(2);
2525
expect(entryQuery()->cache()->all()->count())->toBe(2);

tests/Database/Queries/Concerns/CollectsCacheTagsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use craft\elements\Entry as EntryElement;
43
use CraftCms\Cms\Database\Queries\Events\DefineCacheTags;
4+
use CraftCms\Cms\Element\Elements\Entry as EntryElement;
55
use CraftCms\Cms\Entry\Models\Entry;
66
use Illuminate\Support\Facades\Event;
77

tests/Database/Queries/Concerns/QueriesCustomFieldsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use craft\behaviors\CustomFieldBehavior;
4-
use craft\elements\Entry;
54
use craft\fieldlayoutelements\CustomField;
5+
use CraftCms\Cms\Element\Elements\Entry;
66
use CraftCms\Cms\Entry\Models\Entry as EntryModel;
77
use CraftCms\Cms\Field\Models\Field;
88
use CraftCms\Cms\Field\PlainText;
@@ -48,7 +48,7 @@
4848

4949
Fields::refreshFields();
5050

51-
/** @var \craft\elements\Entry $entry */
51+
/** @var \CraftCms\Cms\Element\Elements\Entry $entry */
5252
$entry = entryQuery()->first();
5353
$entry->title = 'Test entry';
5454
$entry->setFieldValue('textField', 'Foo');

tests/Database/Queries/Concerns/QueriesEagerlyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
use craft\behaviors\CustomFieldBehavior;
44
use craft\elements\ElementCollection;
5-
use craft\elements\Entry;
65
use craft\fieldlayoutelements\CustomField;
76
use CraftCms\Cms\Database\Queries\ElementQuery;
7+
use CraftCms\Cms\Element\Elements\Entry;
88
use CraftCms\Cms\Element\Models\EntryType;
99
use CraftCms\Cms\Entry\Models\Entry as EntryModel;
1010
use CraftCms\Cms\Field\Entries;

tests/Database/Queries/Concerns/QueriesNestedElementsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
/** @var \CraftCms\DependencyAwareCache\Dependency\TagDependency $dependency */
5151
$dependency = Craft::$app->getElements()->stopCollectingCacheInfo()[0];
5252

53-
expect($dependency->tags)->toContain('element::craft\elements\Entry::field:'.$field->id);
53+
expect($dependency->tags)->toContain('element::CraftCms\Cms\Element\Elements\Entry::field:'.$field->id);
5454
expect($dependency->tags)->toContain('element::'.$entry->id);
5555
});

tests/Database/Queries/Concerns/QueriesRelatedElementsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use craft\behaviors\CustomFieldBehavior;
4-
use craft\elements\Entry;
54
use craft\fieldlayoutelements\CustomField;
5+
use CraftCms\Cms\Element\Elements\Entry;
66
use CraftCms\Cms\Entry\Models\Entry as EntryModel;
77
use CraftCms\Cms\Field\Entries;
88
use CraftCms\Cms\Field\Models\Field;

0 commit comments

Comments
 (0)