Skip to content

Commit

Permalink
[Bug]: Omit Mandatory Check on field with Default Value when creating…
Browse files Browse the repository at this point in the history
… a new DataObject (pimcore#15812)

* skip check on mandatory fields with default value on Add

* add tests and intentionally crash it

* try to pass it

* clean up

* remove unnecesary getMandatory check
  • Loading branch information
kingjia90 authored Aug 29, 2023
1 parent ae651bd commit c6b1446
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions models/DataObject/Concrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ protected function update(bool $isUpdate = null, array $params = []): void

if (method_exists($this, $getter)) {
$value = $this->$getter();

$omitMandatoryCheck = $this->getOmitMandatoryCheck();
// when adding a new object, skip check on mandatory fields with default value
if (empty($value) && !$isUpdate && method_exists($fd, 'getDefaultValue') && !empty($fd->getDefaultValue())
){
$omitMandatoryCheck = true;
}

//check throws Exception
try {
Expand Down
15 changes: 15 additions & 0 deletions tests/Model/DataObject/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ public function testDefaultValueSavedToVersion(): void
$this->assertEquals('default', $latestVersion->getData()->getInputWithDefault(), 'Expected default value saved to version');
}

/**
* Verifies a newly published object gets the default values of mandatory fields
*/
public function testDefaultValueAndMandatorySavedToVersion(): void
{
$object = TestHelper::createEmptyObject('', false, true);
$object->setOmitMandatoryCheck(false);
$object->save();

$versions = $object->getVersions();
$latestVersion = end($versions);

$this->assertEquals('default', $latestVersion->getData()->getMandatoryInputWithDefault(), 'Expected default value saved to version');
}

/**
* Verifies that when an object gets cloned, the fields get copied properly
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/Support/Helper/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ public function setupPimcoreClass_Unittest(string $name = 'unittest', string $fi
$inputWithDefault = $this->createDataChild('input', 'inputWithDefault');
$inputWithDefault->setDefaultValue('default');
$panel->addChild($inputWithDefault);
/** @var ClassDefinition\Data\Input $mandatoryInputWithDefault */
$mandatoryInputWithDefault = $this->createDataChild('input', 'mandatoryInputWithDefault', true);
$mandatoryInputWithDefault->setDefaultValue('default');
$panel->addChild($mandatoryInputWithDefault);

$panel->addChild($this->createDataChild('manyToOneRelation', 'lazyHref')
->setDocumentTypes([])->setAssetTypes([])->setClasses([])
Expand Down

0 comments on commit c6b1446

Please sign in to comment.