diff --git a/plugin/Field/DefaultValue/DefaultValueRead.php b/plugin/Field/DefaultValue/DefaultValueRead.php index beb667800..ad85cbac3 100644 --- a/plugin/Field/DefaultValue/DefaultValueRead.php +++ b/plugin/Field/DefaultValue/DefaultValueRead.php @@ -23,7 +23,6 @@ namespace onOffice\WPlugin\Field\DefaultValue; -use onOffice\WPlugin\Types\Field; use wpdb; use const OBJECT; use function esc_sql; @@ -51,104 +50,40 @@ public function __construct(wpdb $pWPDB) /** * @param int $formId - * @param Field $pField - * @return DefaultValueModelSingleselect + * @param array $pFields + * @return array */ - public function readDefaultValuesSingleselect(int $formId, Field $pField): DefaultValueModelSingleselect + public function readDefaultMultiValuesSingleSelect(int $formId, array $pFields): array { - $query = $this->createBaseQuery($formId, $pField); - $row = $this->_pWPDB->get_row($query, ARRAY_A); - $pDataModel = new DefaultValueModelSingleselect($formId, $pField); - $pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0); - $pDataModel->setValue(!empty($row['value']) ? $row['value'] : ''); - return $pDataModel; + $pFieldNames = array_map(function ($field) { + return $field->getName(); + }, $pFields); + $query = $this->createMultiBaseQuery($formId, $pFieldNames); + return $this->_pWPDB->get_results($query, OBJECT); } /** * @param int $formId - * @param Field $pField - * @return DefaultValueModelMultiselect - */ - public function readDefaultValuesMultiSelect(int $formId, Field $pField): DefaultValueModelMultiselect - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, ARRAY_A); - $values = array_column($rows, 'value'); - $pDataModel = new DefaultValueModelMultiselect($formId, $pField); - $pDataModel->setValues($values); - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelBool - */ - public function readDefaultValuesBool(int $formId, Field $pField): DefaultValueModelBool - { - $query = $this->createBaseQuery($formId, $pField); - $row = $this->_pWPDB->get_row($query, ARRAY_A); - $pDataModel = new DefaultValueModelBool($formId, $pField); - $pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0); - $pDataModel->setValue(!empty($row['value']) ? (bool)intval($row['value']) : false); - - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelText - */ - public function readDefaultValuesText(int $formId, Field $pField): DefaultValueModelText - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, OBJECT); - $pDataModel = new DefaultValueModelText($formId, $pField); - - foreach ($rows as $pRow) { - $pDataModel->addValueByLocale($pRow->locale, $pRow->value); - } - - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelNumericRange - */ - public function readDefaultValuesNumericRange(int $formId, Field $pField): DefaultValueModelNumericRange - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, OBJECT); - $pDataModel = new DefaultValueModelNumericRange($formId, $pField); - - if (count($rows) === 2) { - $pDataModel->setValueFrom((float)$rows[0]->value); - $pDataModel->setValueTo((float)$rows[1]->value); - } - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField + * @param array $pFieldNames * @return string */ - private function createBaseQuery(int $formId, Field $pField): string + private function createMultiBaseQuery(int $formId, array $pFieldNames): string { + $names = array_map(function ($v) { + return "'" . esc_sql($v) . "'"; + }, $pFieldNames); + $names = implode(',', $names); + $prefix = $this->_pWPDB->prefix; - $query = "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id," - ."{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n" - ."{$prefix}oo_plugin_fieldconfig_form_defaults_values.value\n" - ."FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n" - ."INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n" - ."ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = " - ." {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n" - ."WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname = '".esc_sql($pField->getName())."' AND\n" - ." {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = ".esc_sql($formId); - return $query; + return "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id," + . "{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n" + . "{$prefix}oo_plugin_fieldconfig_form_defaults_values.value,\n" + . "{$prefix}oo_plugin_fieldconfig_form_defaults.fieldname\n" + . "FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n" + . "INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n" + . "ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = " + . " {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n" + . "WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname IN (" . $names . ") AND\n" + . " {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = " . esc_sql($formId); } - } diff --git a/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php b/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php index fe79e8505..6b11ef864 100644 --- a/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php +++ b/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php @@ -25,6 +25,11 @@ use DI\DependencyException; use DI\NotFoundException; +use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelBool; +use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelMultiselect; +use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelNumericRange; +use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelSingleselect; +use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelText; use onOffice\WPlugin\Field\DefaultValue\DefaultValueRead; use onOffice\WPlugin\Types\Field; use onOffice\WPlugin\Types\FieldTypes; @@ -57,112 +62,197 @@ public function __construct( $this->_pDefaultValueReader = $pDefaultValueReader; } - /** * @param int $formId - * @param Field $pField + * @param array $pFields * @return array * @throws DependencyException * @throws NotFoundException */ - public function getConvertedField(int $formId, Field $pField): array + public function getConvertedMultiFields(int $formId, array $pFields): array { - $isSingleValue = FieldTypes::isDateOrDateTime($pField->getType()) || - FieldTypes::isNumericType($pField->getType()) || - $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT; - $isMultiSelect = $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT; - $isBoolean = $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN; - $isStringType = FieldTypes::isStringType($pField->getType()); - $isRegZusatz = FieldTypes::isRegZusatzSearchcritTypes($pField->getType()); - - if ($pField->getIsRangeField()) { - return $this->convertNumericRange($formId, $pField); - } elseif ($isSingleValue) { - return $this->convertGeneric($formId, $pField); - } elseif ($isMultiSelect) { - return $this->convertMultiSelect($formId, $pField); - } elseif ($isBoolean) { - return $this->convertBoolean($formId, $pField); - } elseif ($isStringType) { - return $this->convertText($formId, $pField); - } elseif ($isRegZusatz) { - $pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); - return $this->convertMultiSelect($formId, $pField); + $pDataModels = []; + $rows = $this->_pDefaultValueReader->readDefaultMultiValuesSingleSelect($formId, $pFields); + + foreach ($pFields as $pField) { + $rowData = array_values(array_filter($rows, function ($row) use ($pField) { + return $pField->getName() == $row->fieldname; + })); + + if (count($rowData)) { + switch ($pField) { + case $pField->getIsRangeField(): + $pDataModel = $this->convertNumericRange($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + $pDataModels[ $pField->getName() . '__von' ] = $pDataModel['min'] ?? ''; + $pDataModels[ $pField->getName() . '__bis' ] = $pDataModel['max'] ?? ''; + break; + case FieldTypes::isDateOrDateTime($pField->getType()): + case FieldTypes::isNumericType($pField->getType()): + case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT: + $pDataModel = $this->convertGeneric($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + break; + case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT; + $pDataModel = $this->convertMultiSelect($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + $pDataModels[ $pField->getName() ] = $pDataModel; + break; + case $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN; + $pDataModel = $this->convertBoolean($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + break; + case FieldTypes::isStringType($pField->getType()); + $pDataModel = $this->convertText($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + $pDataModels[ $pField->getName() ] = ($pDataModel['native'] ?? '') ?: (array_shift($pDataModel) ?? ''); + break; + case FieldTypes::isRegZusatzSearchcritTypes($pField->getType()); + $pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); + $pDataModel = $this->convertMultiSelect($formId, $pField, $rowData); + $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; + break; + } + } } - return []; + + return $pDataModels; } + /** + * @throws DependencyException + * @throws NotFoundException + */ + public function getConvertedMultiFieldsForAdmin(int $formId, array $pFields): array + { + $pDataModels = []; + $rows = $this->_pDefaultValueReader->readDefaultMultiValuesSingleSelect($formId, $pFields); + + foreach ($pFields as $pField) { + $rowData = array_values(array_filter($rows, function ($row) use ($pField) { + return $pField->getName() == $row->fieldname; + })); + switch ($pField) { + case $pField->getIsRangeField(): + $pDataModel = $this->convertNumericRange($formId, $pField, $rowData); + break; + case FieldTypes::isDateOrDateTime($pField->getType()): + case FieldTypes::isNumericType($pField->getType()): + case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT: + $pDataModel = $this->convertGeneric($formId, $pField, $rowData); + break; + case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT; + $pDataModel = $this->convertMultiSelect($formId, $pField, $rowData); + break; + case $pField->getType() === FieldTypes::FIELD_TYPE_BOOLEAN; + $pDataModel = $this->convertBoolean($formId, $pField, $rowData); + break; + case FieldTypes::isStringType($pField->getType()); + $pDataModel = $this->convertText($formId, $pField, $rowData); + break; + case FieldTypes::isRegZusatzSearchcritTypes($pField->getType()); + $pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); + $pDataModel = $this->convertMultiSelect($formId, $pField, $rowData); + break; + } + if (isset($pDataModel)) $pDataModels[ $pField->getName() ] = $pDataModel; + } + + return $pDataModels; + } /** - * * @param int $formId * @param Field $pField + * @param array $rows * @return array * @throws DependencyException * @throws NotFoundException - * */ - - private function convertGeneric(int $formId, Field $pField): array + private function convertNumericRange(int $formId, Field $pField, array $rows): array { - $pModel = $this->_pDefaultValueReader->readDefaultValuesSingleselect($formId, $pField); - $pConverter = $this->_pOutputConverterFactory->createForSingleSelect(); - return $pConverter->convertToRow($pModel); + $pDataModel = new DefaultValueModelNumericRange($formId, $pField); + + if (count($rows) === 2) { + $pDataModel->setValueFrom((float) $rows[0]->value); + $pDataModel->setValueTo((float) $rows[1]->value); + } + + $pConverter = $this->_pOutputConverterFactory->createForNumericRange(); + return $pConverter->convertToRow($pDataModel); } /** * @param int $formId * @param Field $pField + * @param array $rows * @return array * @throws DependencyException * @throws NotFoundException */ - private function convertMultiSelect(int $formId, Field $pField): array + private function convertGeneric(int $formId, Field $pField, array $rows): array { - $pModel = $this->_pDefaultValueReader->readDefaultValuesMultiSelect($formId, $pField); - $pConverter = $this->_pOutputConverterFactory->createForMultiSelect(); - return $pConverter->convertToRow($pModel); + $pDataModel = new DefaultValueModelSingleselect($formId, $pField); + $pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int) $rows[0]->defaults_id : 0); + $pDataModel->setValue($rows[0]->value ?? ''); + + $pConverter = $this->_pOutputConverterFactory->createForSingleSelect(); + return $pConverter->convertToRow($pDataModel); } /** * @param int $formId * @param Field $pField + * @param array $rows * @return array * @throws DependencyException * @throws NotFoundException */ - private function convertText(int $formId, Field $pField): array + private function convertMultiSelect(int $formId, Field $pField, array $rows): array { - $pModel = $this->_pDefaultValueReader->readDefaultValuesText($formId, $pField); - $pConverter = $this->_pOutputConverterFactory->createForText(); - return $pConverter->convertToRow($pModel); + $values = array_column($rows, 'value'); + $pDataModel = new DefaultValueModelMultiselect($formId, $pField); + $pDataModel->setValues($values); + + $pConverter = $this->_pOutputConverterFactory->createForMultiSelect(); + return $pConverter->convertToRow($pDataModel); } /** * @param int $formId * @param Field $pField + * @param array $rows * @return array * @throws DependencyException * @throws NotFoundException */ - private function convertNumericRange(int $formId, Field $pField): array + private function convertBoolean(int $formId, Field $pField, array $rows): array { - $pModel = $this->_pDefaultValueReader->readDefaultValuesNumericRange($formId, $pField); - $pConverter = $this->_pOutputConverterFactory->createForNumericRange(); - return $pConverter->convertToRow($pModel); + $pDataModel = new DefaultValueModelBool($formId, $pField); + $pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int)$rows[0]->defaults_id : 0); + $pDataModel->setValue(isset($rows[0]->value) && !empty($rows[0]->value) && (bool)intval($rows[0]->value)); + + $pConverter = $this->_pOutputConverterFactory->createForBool(); + return $pConverter->convertToRow($pDataModel); } /** * @param int $formId * @param Field $pField + * @param array $rows * @return array * @throws DependencyException * @throws NotFoundException */ - private function convertBoolean(int $formId, Field $pField): array + private function convertText(int $formId, Field $pField, array $rows): array { - $pModel = $this->_pDefaultValueReader->readDefaultValuesBool($formId, $pField); - $pConverter = $this->_pOutputConverterFactory->createForBool(); - return $pConverter->convertToRow($pModel); + $pDataModel = new DefaultValueModelText($formId, $pField); + + foreach ($rows as $pRow) { + $pDataModel->addValueByLocale($pRow->locale, $pRow->value); + } + + $pConverter = $this->_pOutputConverterFactory->createForText(); + return $pConverter->convertToRow($pDataModel); } } \ No newline at end of file diff --git a/plugin/Form.php b/plugin/Form.php index f16f983b1..5ff66acf8 100644 --- a/plugin/Form.php +++ b/plugin/Form.php @@ -551,18 +551,9 @@ private function getDefaultValues(): array $formId = $this->getDataFormConfiguration()->getId(); $values = []; - foreach ($this->_pFieldsCollection->getAllFields() as $pField) { - $value = $pDefaultValueRead->getConvertedField($formId, $pField); - $values[$pField->getName()] = $value[0] ?? ''; - - if ($pField->getIsRangeField()) { - $values[$pField->getName().'__von'] = $value['min'] ?? ''; - $values[$pField->getName().'__bis'] = $value['max'] ?? ''; - } elseif ($pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT) { - $values[$pField->getName()] = $value; - } elseif (FieldTypes::isStringType($pField->getType())) { - $values[$pField->getName()] = ($value['native'] ?? '') ?: (array_shift($value) ?? ''); - } + foreach (array_chunk($this->_pFieldsCollection->getAllFields(), 100) as $fields) { + $pDefaultFields = $pDefaultValueRead->getConvertedMultiFields($formId, $fields); + if (count($pDefaultFields)) $values = array_merge($values, $pDefaultFields); } return array_filter($values); } diff --git a/plugin/Gui/AdminPageFormSettingsBase.php b/plugin/Gui/AdminPageFormSettingsBase.php index ddc536e84..94c4271ca 100644 --- a/plugin/Gui/AdminPageFormSettingsBase.php +++ b/plugin/Gui/AdminPageFormSettingsBase.php @@ -366,6 +366,7 @@ private function getFieldList(): array * @return array * @throws DependencyException * @throws NotFoundException + * @throws UnknownFieldException */ private function readDefaultValues(): array { @@ -373,9 +374,9 @@ private function readDefaultValues(): array /** @var DefaultValueModelToOutputConverter $pDefaultValueConverter */ $pDefaultValueConverter = $this->getContainer()->get(DefaultValueModelToOutputConverter::class); - foreach ($this->buildFieldsCollectionForCurrentForm()->getAllFields() as $pField) { - $result[$pField->getName()] = $pDefaultValueConverter->getConvertedField - ((int)$this->getListViewId(), $pField); + foreach (array_chunk($this->buildFieldsCollectionForCurrentForm()->getAllFields(), 100) as $pField) { + $pDefaultFields = $pDefaultValueConverter->getConvertedMultiFieldsForAdmin((int) $this->getListViewId(), $pField); + if (count($pDefaultFields)) $result = array_merge($result, $pDefaultFields); } return $result; } diff --git a/tests/TestClassDefaultValueModelToOutputConverter.php b/tests/TestClassDefaultValueModelToOutputConverter.php index a114199c8..80bc117d9 100644 --- a/tests/TestClassDefaultValueModelToOutputConverter.php +++ b/tests/TestClassDefaultValueModelToOutputConverter.php @@ -27,11 +27,6 @@ use DI\ContainerBuilder; use DI\DependencyException; use DI\NotFoundException; -use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelBool; -use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelMultiselect; -use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelNumericRange; -use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelSingleselect; -use onOffice\WPlugin\Field\DefaultValue\DefaultValueModelText; use onOffice\WPlugin\Field\DefaultValue\DefaultValueRead; use onOffice\WPlugin\Field\DefaultValue\ModelToOutputConverter\DefaultValueModelToOutputConverter; use onOffice\WPlugin\Types\Field; @@ -81,11 +76,40 @@ public function prepare() * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForUnknownFieldType() + public function testGetConvertedMultiFieldsForNonEmptyNumericRangeField() { - $this->_pField->setType('unknown'); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEmpty($result); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => '11.0', + 'fieldname' => 'testField', + ], + (object)[ + 'defaults_id' => '13', + 'value' => '14.0', + 'fieldname' => 'testField', + ], + (object)[ + 'defaults_id' => '16', + 'value' => 'abc', + 'fieldname' => 'fieldname4', + 'type' => 'singleselect', + 'locale' => 'en_US', + ], + (object)[ + 'defaults_id' => '17', + 'value' => 'abc', + 'fieldname' => 'fieldname5', + 'type' => 'singleselect', + 'locale' => 'en_US', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $this->_pField->setIsRangeField(true); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField__von'=>'11.0', 'testField__bis'=>'14.0', 'testField'=>''], $result); } @@ -96,62 +120,106 @@ public function testGetConvertedFieldForUnknownFieldType() * */ - public function testGetConvertedFieldForEmptyTextField() + public function testGetConvertedMultiFieldsForNonEmptySingleSelectField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_TEXT); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEmpty($result); + $this->_pField->setType(FieldTypes::FIELD_TYPE_SINGLESELECT); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Monday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => 'en_US', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField'=> 'Monday'], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForBoolField() + public function testGetConvertedMultiFieldsForNonEmptyMultiSelectField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_BOOLEAN); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['0'], $result); + $this->_pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Monday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + (object)[ + 'defaults_id' => '13', + 'value' => 'Tuesday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + (object)[ + 'defaults_id' => '13', + 'value' => 'Wednesday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField'=> ['Monday','Tuesday','Wednesday']], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForNonEmptyBoolField() + public function testGetConvertedMultiFieldsForNonEmptyBoolField() { $this->_pField->setType(FieldTypes::FIELD_TYPE_BOOLEAN); - $pBoolFieldModel = new DefaultValueModelBool(13, $this->_pField); - $pBoolFieldModel->setValue(true); - + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => true, + 'fieldname' => 'testField', + 'type' => 'boolean', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesBool')->will($this->returnValue($pBoolFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['1'], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField'=> true], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForNonEmptyTextField() + public function testGetConvertedMultiFieldsForNonEmptyTextField() { $this->_pField->setType(FieldTypes::FIELD_TYPE_TEXT); - $pTextFieldModel = new DefaultValueModelText(13, $this->_pField); - $pTextFieldModel->addValueByLocale('de_DE', 'Die menschliche Spinne'); - $pTextFieldModel->addValueByLocale('en_US', 'Spider Man'); - $pTextFieldModel->addValueByLocale('fr_BE', 'Homme araignée'); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Spider Man', + 'fieldname' => 'testField', + 'type' => 'text', + 'locale' => 'native', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesText')->will($this->returnValue($pTextFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals([ - 'de_DE' => 'Die menschliche Spinne', - 'fr_BE' => 'Homme araignée', - 'native' => 'Spider Man', - ], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField' => 'Spider Man'], $result); } @@ -162,103 +230,191 @@ public function testGetConvertedFieldForNonEmptyTextField() * */ - public function testGetConvertedFieldForEmptySingleSelectField() + public function testGetConvertedMultiFieldsForRegZusatz() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_SINGLESELECT); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals([''], $result); + $this->_pField->setType('displayAll'); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Aachen', + 'fieldname' => 'testField', + 'type' => 'displayAll', + 'locale' => '', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->getConvertedMultiFields(13, [$this->_pField]); + $this->assertEquals(['testField' => 'Aachen'], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForEmptyMultiSelectField() + public function testGetConvertedMultiFieldsForAdminForNonEmptyNumericRangeField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertSame([], $result); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => '11.0', + 'fieldname' => 'testField', + ], + (object)[ + 'defaults_id' => '13', + 'value' => '14.0', + 'fieldname' => 'testField', + ], + (object)[ + 'defaults_id' => '16', + 'value' => 'abc', + 'fieldname' => 'fieldname4', + 'type' => 'singleselect', + 'locale' => 'en_US', + ], + (object)[ + 'defaults_id' => '17', + 'value' => 'abc', + 'fieldname' => 'fieldname5', + 'type' => 'singleselect', + 'locale' => 'en_US', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $this->_pField->setIsRangeField(true); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField'=>['min'=>'11.0', 'max'=>'14.0']], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForNonEmptyMultiSelectField() + public function testGetConvertedMultiFieldsForAdminForNonEmptySingleSelectField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); - $pMultiSelectFieldModel = new DefaultValueModelMultiselect(13, $this->_pField); - $pMultiSelectFieldModel->setValues(['Monday', 'Tuesday', 'Wednesday', 'Saturday']); - + $this->_pField->setType(FieldTypes::FIELD_TYPE_SINGLESELECT); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Monday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => 'en_US', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesMultiSelect')->will($this->returnValue($pMultiSelectFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['Monday', 'Tuesday', 'Wednesday', 'Saturday'], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField'=> ['Monday']], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForRegZusatz() + public function testGetConvertedMultiFieldsForAdminForNonEmptyMultiSelectField() { - $this->_pField->setType('displayAll'); - $pMultiSelectFieldModel = new DefaultValueModelMultiselect(13, $this->_pField); - $pMultiSelectFieldModel->setValues(['Aachen', 'Würselen', 'Herzogenrath']); - + $this->_pField->setType(FieldTypes::FIELD_TYPE_MULTISELECT); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Monday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + (object)[ + 'defaults_id' => '13', + 'value' => 'Tuesday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + (object)[ + 'defaults_id' => '13', + 'value' => 'Wednesday', + 'fieldname' => 'testField', + 'type' => 'date', + 'locale' => '', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesMultiSelect')->will($this->returnValue($pMultiSelectFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['Aachen', 'Würselen', 'Herzogenrath'], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField' => ['Monday', 'Tuesday', 'Wednesday']], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForNonEmptySingleSelectField() + public function testGetConvertedMultiFieldsForAdminForNonEmptyBoolField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_SINGLESELECT); - $pSingleSelectFieldModel = new DefaultValueModelSingleselect(13, $this->_pField); - $pSingleSelectFieldModel->setValue('Monday'); - + $this->_pField->setType(FieldTypes::FIELD_TYPE_BOOLEAN); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => true, + 'fieldname' => 'testField', + 'type' => 'boolean', + 'locale' => '', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesSingleselect')->will($this->returnValue($pSingleSelectFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['Monday'], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField' => [true]], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForNonEmptyNumericRangeField() + public function testGetConvertedMultiFieldsForAdminForNonEmptyTextField() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_FLOAT); - $this->_pField->setIsRangeField(true); - $pRangeFieldModel = new DefaultValueModelNumericRange(13, $this->_pField); - $pRangeFieldModel->setValueFrom(11.); - $pRangeFieldModel->setValueTo(14.); - + $this->_pField->setType(FieldTypes::FIELD_TYPE_TEXT); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Spider Man', + 'fieldname' => 'testField', + 'type' => 'text', + 'locale' => 'native', + ], + ]; $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); $pDefaultValueReader->expects($this->once()) - ->method('readDefaultValuesNumericRange')->will($this->returnValue($pRangeFieldModel)); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['min' => 11.0, 'max' => 14.0], $result); + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField' => ['native' => 'Spider Man']], $result); } /** * @throws DependencyException * @throws NotFoundException */ - public function testGetConvertedFieldForEmptyNumericRangeField() + public function testGetConvertedMultiFieldsForAdminForRegZusatz() { - $this->_pField->setType(FieldTypes::FIELD_TYPE_FLOAT); - $this->_pField->setIsRangeField(true); - $result = $this->_pSubject->getConvertedField(13, $this->_pField); - $this->assertEquals(['min' => .0, 'max' => .0], $result); + $this->_pField->setType('displayAll'); + $row = [ + (object)[ + 'defaults_id' => '13', + 'value' => 'Aachen', + 'fieldname' => 'testField', + 'type' => 'displayAll', + 'locale' => '', + ], + ]; + $pDefaultValueReader = $this->_pContainer->get(DefaultValueRead::class); + $pDefaultValueReader->expects($this->once()) + ->method('readDefaultMultiValuesSingleSelect')->will($this->returnValue($row)); + $result = $this->_pSubject->GetConvertedMultiFieldsForAdmin(13, [$this->_pField]); + $this->assertEquals(['testField' => ['Aachen']], $result); } } diff --git a/tests/TestClassDefaultValueRead.php b/tests/TestClassDefaultValueRead.php index 34620a254..ff28b7d9b 100644 --- a/tests/TestClassDefaultValueRead.php +++ b/tests/TestClassDefaultValueRead.php @@ -74,23 +74,6 @@ public function prepare() * */ - public function testReadDefaultValuesSingleselect(int $formId, int $defaultValueId, string $value) - { - $row = [ - 'defaults_id' => $defaultValueId, - 'value' => $value, - ]; - $this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row)); - $pField = new Field('testField', 'testModule'); - $pExpectedDataModel = new DefaultValueModelSingleselect($formId, $pField); - $pExpectedDataModel->setValue($value); - $pExpectedDataModel->setDefaultsId($defaultValueId); - $pResult = $this->_pSubject->readDefaultValuesSingleselect($formId, $pField); - $this->assertInstanceOf(DefaultValueModelSingleselect::class, $pResult); - $this->assertEquals($pExpectedDataModel, $pResult); - } - - /** * * @return array @@ -105,21 +88,6 @@ public function dataProviderSingleSelect(): array ]; } - /** - * @dataProvider dataProviderMultiSelect - * @param int $formId - * @param array $rows - * @param DefaultValueModelMultiselect $pReference - */ - public function testReadDefaultValuesMultiSelect(int $formId, array $rows, DefaultValueModelMultiselect $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - $pResult = $this->_pSubject->readDefaultValuesMultiSelect($formId, $pField); - $this->assertInstanceOf(DefaultValueModelMultiselect::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * @return Generator */ @@ -160,16 +128,6 @@ public function dataProviderMultiSelect(): Generator * */ - public function testReadDefaultValuesText(int $formId, array $rows, DefaultValueModelText $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - - $pResult = $this->_pSubject->readDefaultValuesText($formId, $pField); - $this->assertInstanceOf(DefaultValueModelText::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * * @return Generator @@ -206,22 +164,6 @@ public function dataProviderText(): Generator yield [13, $rows, $pReference2]; } - /** - * @dataProvider dataProviderNumericRange - * @param int $formId - * @param array $rows - * @param DefaultValueModelNumericRange $pReference - */ - public function testReadDefaultValuesNumericRange(int $formId, array $rows, DefaultValueModelNumericRange $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - - $pResult = $this->_pSubject->readDefaultValuesNumericRange($formId, $pField); - $this->assertInstanceOf(DefaultValueModelNumericRange::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * @return array */ @@ -247,24 +189,6 @@ public function dataProviderNumericRange(): array ]; } - /** - * @dataProvider dataProviderBool - * @param int $formId - * @param array $row - * @param bool $expectedResult - */ - public function testReadDefaultValuesBool(int $formId, array $row, bool $expectedResult) - { - $this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row)); - $pField = new Field('testField', 'testModule'); - $pExpectedDataModel = new DefaultValueModelBool($formId, $pField); - $pExpectedDataModel->setValue($expectedResult); - $pExpectedDataModel->setDefaultsId((int)$row['defaults_id']); - $pResult = $this->_pSubject->readDefaultValuesBool($formId, $pField); - $this->assertInstanceOf(DefaultValueModelBool::class, $pResult); - $this->assertEquals($pExpectedDataModel, $pResult); - } - /** * @return array */ @@ -275,4 +199,25 @@ public function dataProviderBool(): array [123, ['defaults_id' => '1334', 'value' => '1'], true], ]; } + + /** + * + * @dataProvider dataProviderSingleSelect + * @param int $formId + * @param int $defaultValueId + * @param string $value + * + */ + + public function testReadDefaultMultiValuesSingleSelect(int $formId, int $defaultValueId, string $value) + { + $row = (object)[ + 'defaults_id' => $defaultValueId, + 'value' => $value, + ]; + $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue([$row])); + $pField = new Field('testField', 'testModule'); + $pResult = $this->_pSubject->readDefaultMultiValuesSingleSelect($formId, [$pField]); + $this->assertEquals([$row], $pResult); + } } diff --git a/tests/TestClassForm.php b/tests/TestClassForm.php index 54c2cd5bb..d620a6648 100644 --- a/tests/TestClassForm.php +++ b/tests/TestClassForm.php @@ -44,7 +44,7 @@ class TestClassForm private $_pContainer = null; /** @var Form */ - private $_pSubject = null; + private $_pSubjects = null; /** * @throws Exception @@ -101,11 +101,11 @@ public function prepare() ->getMock(); $this->_pContainer->set(DefaultValueModelToOutputConverter::class, $pDefaultValueModelToOutputConverter); - $pDefaultValueModelToOutputConverter->expects($this->atLeastOnce())->method('getConvertedField') + $pDefaultValueModelToOutputConverter->expects($this->atLeastOnce())->method('getConvertedMultiFields') ->will($this->onConsecutiveCalls(['testValue'], ['testMulti1', 'testMulti2'], [['min' => 13.1, 'max' => 14.]])); add_option('onoffice-settings-honeypot', true); - $this->_pSubject = new Form('testForm1', Form::TYPE_INTEREST, $this->_pContainer); + $this->_pSubjects = new Form('testForm1', Form::TYPE_INTEREST, $this->_pContainer); } /** @@ -113,11 +113,13 @@ public function prepare() */ public function testGetFieldValueWithDefaultValue() { - $this->assertEquals('testValue', $this->_pSubject->getFieldValue('testFieldText')); - $this->assertEquals(['testMulti1', 'testMulti2'], - $this->_pSubject->getFieldValue('testFieldMultiSelect', true)); - $this->assertEquals(['min' => 13.1, 'max' => 14.], - $this->_pSubject->getFieldValue('testRange', true)); + foreach ($this->_pSubjects as $pSubject) { + $this->assertEquals('testValue', $pSubject->getFieldValue('testFieldText')); + $this->assertEquals(['testMulti1', 'testMulti2'], + $pSubject->getFieldValue('testFieldMultiSelect', true)); + $this->assertEquals(['min' => 13.1, 'max' => 14.], + $pSubject->getFieldValue('testRange', true)); + } } /**