Skip to content

EspoCRM – Open Source CRM Application

Import test

Yurii77260dd8cbf8
diff --git a/application/Espo/Tools/Import/Import.php b/application/Espo/Tools/Import/Import.php
index 2c568b3c3d..f313deeb54 100644
--- a/application/Espo/Tools/Import/Import.php
+++ b/application/Espo/Tools/Import/Import.php
@@ -484,7 +484,7 @@ class Import
$isNew = $entity->isNew();
- $entity->setMultiple($params->getDefaultValues());
+ $entity->setMultiple($this->filterValuesMap($params->getDefaultValues()));
// Values are not supposed to be sanitized with the field Sanitizer.
$valueMap = $this->prepareRowValueMap($attributeList, $row);
@@ -1557,4 +1557,31 @@ class Import
unset($attributeList[$attribute]);
}
}
+
+ private function filterValuesMap(stdClass $values): stdClass
+ {
+ $forbiddenAttributeList = $this->getEditForbiddenAttributeList();
+
+ $output = (object) [];
+
+ foreach (get_object_vars($values) as $attribute => $value) {
+ if (in_array($attribute, $forbiddenAttributeList)) {
+ continue;
+ }
+
+ $output->$attribute = $value;
+ }
+
+ return $output;
+ }
+
+ /**
+ * @return string[]
+ */
+ private function getEditForbiddenAttributeList(): array
+ {
+ $entityType = $this->entityType ?? throw new LogicException();
+
+ return $this->aclManager->getScopeForbiddenAttributeList($this->user, $entityType, Table::ACTION_EDIT);
+ }
}
diff --git a/tests/integration/Espo/Tools/Import/ImportTest.php b/tests/integration/Espo/Tools/Import/ImportTest.php
index f65b8b583b..377be290c2 100644
--- a/tests/integration/Espo/Tools/Import/ImportTest.php
+++ b/tests/integration/Espo/Tools/Import/ImportTest.php
@@ -177,4 +177,76 @@ class ImportTest extends BaseTestCase
$this->assertNotNull($contact);
$this->assertEquals($account->getId(), $contact->getAccount()?->getId());
}
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testDefaultValues(): void
+ {
+ $this->createUser('test', [
+ Role::FIELD_DATA => [
+ 'Import' => true,
+ Account::ENTITY_TYPE => [
+ Table::ACTION_CREATE => Table::LEVEL_YES,
+ Table::ACTION_READ => Table::LEVEL_ALL,
+ ],
+ ],
+ Role::FIELD_FIELD_DATA => [
+ Account::ENTITY_TYPE => [
+ 'type' => [
+ Table::ACTION_READ => Table::LEVEL_YES,
+ Table::ACTION_EDIT => Table::LEVEL_NO,
+ ],
+ ]
+ ]
+ ]);
+
+ $this->authenticate('test');
+
+ $em = $this->getEntityManager();
+
+ //
+
+ $attachment = $em->getRDBRepositoryByClass(Attachment::class)->getNew();
+ $attachment
+ ->setName('test.csv')
+ ->setType('text/csv')
+ ->setContents(
+ <<<'EOT'
+ name
+ Test
+ ```
+ EOT
+ );
+ $em->saveEntity($attachment);
+
+ $account = $em->getRDBRepositoryByClass(Account::class)->getNew();
+ $account->setType(Account::TYPE_PARTNER);
+ $em->saveEntity($account);
+
+ $import = $this->getInjectableFactory()->create(Import::class);
+
+ $import
+ ->setEntityType(Account::ENTITY_TYPE)
+ ->setParams(
+ Params::create()->withDefaultValues([
+ 'type' => Account::TYPE_PARTNER,
+ 'description' => 'Test.',
+ ])
+ )
+ ->setAttachmentId($attachment->getId())
+ ->setAttributeList(['name']);
+
+ $import->run();
+
+ //
+
+ $account = $em->getRDBRepositoryByClass(Account::class)
+ ->where(['name' => 'Test'])
+ ->findOne();
+
+ $this->assertNotNull($account);
+ $this->assertNull($account->getType());
+ $this->assertEquals('Test.', $account->getDescription());
+ }
}