Skip to content

Commit

Permalink
Convert multi and single option data to values
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Sep 15, 2023
1 parent 526e36c commit aa720f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Release Notes for Blitz

## 4.5.4 - Unreleased
## 4.5.4 - 2023-09-15

### Fixed

- Fixed a bug in which element query params containing multi and single option data were not being converted to values.
- Fixed a bug in which error exceptions were not being caught when produced by cached element queries during the refresh cache process.

## 4.5.3 - 2023-09-12
Expand Down
20 changes: 18 additions & 2 deletions src/helpers/ElementQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use craft\behaviors\CustomFieldBehavior;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\fields\data\MultiOptionsFieldData;
use craft\fields\data\OptionData;
use craft\helpers\ArrayHelper;
use DateTime;
use ReflectionClass;
Expand Down Expand Up @@ -177,6 +179,7 @@ public static function getNormalizedElementQueryIdParam(mixed $value): mixed

/**
* Copied from Db helper
*
* @see \craft\helpers\Db::_toArray
*/
if (is_string($value)) {
Expand Down Expand Up @@ -362,8 +365,7 @@ private static function _getFilterableElementQueryParams(ElementQuery $elementQu
$elementTypeParams[] = $property;
}

// Ignore params that never change. The `orderBy` attribute is
// extracted separately later.
// Ignore params that never change. The `orderBy` attribute is extracted separately later.
$sourceIdAttribute = ElementTypeHelper::getSourceIdAttribute($elementQuery->elementType);
$ignoreParams = [
$sourceIdAttribute,
Expand Down Expand Up @@ -456,5 +458,19 @@ private static function _convertQueryParamsRecursively(mixed &$value): void
if ($value instanceof DateTime) {
$value = $value->getTimestamp();
}

// Convert OptionData objects to their values (ignoring whether selected).
if ($value instanceof OptionData) {
$value = $value->value;
}

// Convert MultiOptionsFieldData objects to arrays of values (ignoring whether selected).
if ($value instanceof MultiOptionsFieldData) {
$options = $value->getOptions();
$value = [];
foreach ($options as $option) {
$value[] = $option->value;
}
}
}
}
2 changes: 2 additions & 0 deletions tests/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ _Tests the saving of cached values, element cache records and element query reco
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query record with relation field is not saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query record with related to param is saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query record with expression is not saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query record with option field data is converted to value.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query record with multi options field data is converted to array of values.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query cache records are saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query source records with specific source identifiers are saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Element query source records without specific source identifiers are not saved.
Expand Down
32 changes: 32 additions & 0 deletions tests/pest/Feature/GenerateCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use craft\commerce\elements\Product;
use craft\db\FixedOrderExpression;
use craft\elements\Entry;
use craft\fields\data\MultiOptionsFieldData;
use craft\fields\data\OptionData;
use craft\helpers\StringHelper;
use craft\mutex\Mutex;
use putyourlightson\blitz\Blitz;
Expand Down Expand Up @@ -236,6 +238,36 @@
->toHaveRecordCount(0);
});

test('Element query record with option field data is converted to value', function() {
$optionFieldData = new OptionData('One', 1, true);
$elementQuery = Entry::find()->dropdown($optionFieldData);
Blitz::$plugin->generateCache->addElementQuery($elementQuery);

/** @var ElementQueryRecord $record */
$record = ElementQueryRecord::find()->one();
$params = json_decode($record->params);

expect($params->dropdown)
->toEqual(1);
});

test('Element query record with multi options field data is converted to array of values', function() {
$optionFieldData = new MultiOptionsFieldData();
$optionFieldData->setOptions([
new OptionData('One', 1, true),
new OptionData('Two', 2, false),
]);
$elementQuery = Entry::find()->multiSelect($optionFieldData);
Blitz::$plugin->generateCache->addElementQuery($elementQuery);

/** @var ElementQueryRecord $record */
$record = ElementQueryRecord::find()->one();
$params = json_decode($record->params);

expect($params->multiSelect)
->toEqual([1, 2]);
});

test('Element query cache records are saved', function() {
$elementQuery = Entry::find();
Blitz::$plugin->generateCache->addElementQuery($elementQuery);
Expand Down

0 comments on commit aa720f5

Please sign in to comment.