From e479e4eafb7ab441fde0727add952966c6429d60 Mon Sep 17 00:00:00 2001 From: Jedson Melo Date: Thu, 1 Apr 2021 11:34:49 -0300 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20corrige=20erro=20em=20valida=C3=A7?= =?UTF-8?q?=C3=A3o=20da=20vers=C3=A3o=20do=20xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/lang/pt_BR/messages.php | 3 +- src/Rules/CnesXMLDate.php | 14 +---- src/Rules/CnesXMLIdentification.php | 31 ++++------ src/Rules/CnesXMLRule.php | 17 ++++++ src/Rules/CnesXMLVersionXSD.php | 57 +++++++++++++++++++ src/Rules/ZipHasValidCnesXML.php | 11 ++++ .../Unit/Rules/CnesXMLIdentificationTest.php | 42 +------------- tests/Unit/Rules/CnesXMLVersionXSDTest.php | 46 +++++++++++++++ tests/Unit/Rules/ZipHasValidCnesXMLTest.php | 48 ++++++++++++++-- 9 files changed, 190 insertions(+), 79 deletions(-) create mode 100644 src/Rules/CnesXMLRule.php create mode 100644 src/Rules/CnesXMLVersionXSD.php create mode 100644 tests/Unit/Rules/CnesXMLVersionXSDTest.php diff --git a/resources/lang/pt_BR/messages.php b/resources/lang/pt_BR/messages.php index 791fd26..d7187a6 100644 --- a/resources/lang/pt_BR/messages.php +++ b/resources/lang/pt_BR/messages.php @@ -4,7 +4,8 @@ 'max_in_the_zip_file' => 'O arquivo zip deve ter no máximo :quantity arquivo(s).', 'not_empty_zip' => 'O arquivo zip não pode está vazio.', 'zip_has_xml_file' => 'O arquivo zip deve conter um XML.', - 'cnes_xml_identification' => 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a :version_xsd', // phpcs:ignore + 'cnes_xml_identification' => 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.', // phpcs:ignore 'cnes_xml_structure' => 'A estrutura do arquivo XML está inválida.', 'cnes_xml_date' => 'A competência do XML deve ser posterior a data :date.', + 'cnes_xml_version_xsd' => 'A versão do XML deve ser compatível com a :version_xsd', ]; diff --git a/src/Rules/CnesXMLDate.php b/src/Rules/CnesXMLDate.php index 00a98ca..43897f0 100644 --- a/src/Rules/CnesXMLDate.php +++ b/src/Rules/CnesXMLDate.php @@ -4,9 +4,10 @@ use SimpleXMLElement; use Illuminate\Contracts\Validation\Rule; +use Sysvale\ValidationRules\Rules\CnesXMLRule; use Sysvale\ValidationRules\Support\ZipWithXMLHandler; -class CnesXMLDate implements Rule +class CnesXMLDate extends CnesXMLRule implements Rule { private $date; @@ -46,17 +47,6 @@ public function message() ]); } - private function getIdentificationAttributes($xml) - { - $identification = $xml->{'IDENTIFICACAO'}; - - if (empty($identification)) { - return []; - } - - return array_values((array) $identification->attributes())[0]; - } - protected function hasValidDate(SimpleXMLElement $xml) { $identification = $this->getIdentificationAttributes($xml); diff --git a/src/Rules/CnesXMLIdentification.php b/src/Rules/CnesXMLIdentification.php index eaef98b..f308846 100644 --- a/src/Rules/CnesXMLIdentification.php +++ b/src/Rules/CnesXMLIdentification.php @@ -4,13 +4,14 @@ use SimpleXMLElement; use Illuminate\Contracts\Validation\Rule; +use Sysvale\ValidationRules\Rules\CnesXMLRule; use Sysvale\ValidationRules\Support\ZipWithXMLHandler; -class CnesXMLIdentification implements Rule +class CnesXMLIdentification extends CnesXMLRule implements Rule { private $expected_ibge_code; - public function __construct($expected_ibge_code, $version_xsd = '2.1') + public function __construct($expected_ibge_code, $version_xsd = null) { $this->expected_ibge_code = $expected_ibge_code; $this->version_xsd = $version_xsd; @@ -47,28 +48,16 @@ public function message() ]); } - protected function hasValidIdentification(SimpleXMLElement $xml) { - foreach ($xml->children() as $key => $identification) { - if ($key !== 'IDENTIFICACAO') { - return false; - } - - $origin = (string) $identification['ORIGEM']; - $target = (string) $identification['DESTINO']; - $ibge_code = (string) $identification['CO_IBGE_MUN']; - $version_xsd = (string) $identification['VERSION_XSD']; + $identification = $this->getIdentificationAttributes($xml); - if ($origin === 'PORTAL' - && $target === 'ESUS_AB' - && $ibge_code === $this->expected_ibge_code - && $version_xsd === $this->version_xsd - ) { - return true; - } - } + $origin = (string) $identification['ORIGEM'] ?? ''; + $target = (string) $identification['DESTINO'] ?? ''; + $ibge_code = (string) $identification['CO_IBGE_MUN'] ?? ''; - return false; + return $origin === 'PORTAL' + && $target === 'ESUS_AB' + && $ibge_code === $this->expected_ibge_code; } } diff --git a/src/Rules/CnesXMLRule.php b/src/Rules/CnesXMLRule.php new file mode 100644 index 0000000..780d597 --- /dev/null +++ b/src/Rules/CnesXMLRule.php @@ -0,0 +1,17 @@ +{'IDENTIFICACAO'}; + + if (empty($identification)) { + return []; + } + + return array_values((array) $identification->attributes())[0]; + } +} diff --git a/src/Rules/CnesXMLVersionXSD.php b/src/Rules/CnesXMLVersionXSD.php new file mode 100644 index 0000000..fcb8460 --- /dev/null +++ b/src/Rules/CnesXMLVersionXSD.php @@ -0,0 +1,57 @@ +version = $version; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + $zip_handler = resolve(ZipWithXMLHandler::class) + ->buildZip($value->path()); + + $passes = $this->hasValidVersion($zip_handler->getSimpleXMLElement()); + + $zip_handler->closeZip(); + + return $passes; + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return __('SysvaleValidationRules::messages.cnes_xml_version_xsd', [ + 'version_xsd' => $this->version, + ]); + } + + protected function hasValidVersion(SimpleXMLElement $xml) + { + $identification = $this->getIdentificationAttributes($xml); + $current_version_xsd = $identification['VERSION_XSD'] ?? null; + + return $current_version_xsd === $this->version; + } +} diff --git a/src/Rules/ZipHasValidCnesXML.php b/src/Rules/ZipHasValidCnesXML.php index fbf61fc..089bf7c 100644 --- a/src/Rules/ZipHasValidCnesXML.php +++ b/src/Rules/ZipHasValidCnesXML.php @@ -35,6 +35,10 @@ public function passes($attribute, $value) $passes = $passes && $this->hasValidDate($attribute, $value); } + if (isset($this->version_xsd)) { + $passes = $passes && $this->hasValidVersion($attribute, $value); + } + return $passes; } @@ -69,6 +73,13 @@ protected function hasValidDate($attribute, $value) return $this->validateWithRule($rule, $attribute, $value); } + protected function hasValidVersion($attribute, $value) + { + $rule = new CnesXMLVersionXSD($this->version_xsd); + + return $this->validateWithRule($rule, $attribute, $value); + } + private function validateWithRule($rule, $attribute, $value) { $passes = $rule->passes($attribute, $value); diff --git a/tests/Unit/Rules/CnesXMLIdentificationTest.php b/tests/Unit/Rules/CnesXMLIdentificationTest.php index 5847c3a..5b66f07 100644 --- a/tests/Unit/Rules/CnesXMLIdentificationTest.php +++ b/tests/Unit/Rules/CnesXMLIdentificationTest.php @@ -19,46 +19,6 @@ public function testPasses() $this->mockXmlContents(['ibge_code' => $ibge_code]); - $rule = new CnesXMLIdentification($ibge_code, '2.1'); - - $this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); - } - - public function testNotPassesVersionXSD() - { - $ibge_code = '12345'; - - $this->mockXmlContents(['ibge_code' => $ibge_code, 'version_xsd' => '']); - - $rule = new CnesXMLIdentification($ibge_code, '2.1'); - - $this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); - } - - public function testNotPassesVersionXSDDiff21() - { - $ibge_code = '12345'; - $version_xsd = '3.0'; - - $this->mockXmlContents([ - 'ibge_code' => $ibge_code, - 'version_xsd' => "VERSION_XSD=\"$version_xsd\"" - ]); - - $rule = new CnesXMLIdentification($ibge_code, '2.1'); - - $this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); - } - - public function testSecondArgumentIsntPassed() - { - $ibge_code = '12345'; - - $this->mockXmlContents([ - 'ibge_code' => $ibge_code, - 'version_xsd' => "VERSION_XSD=\"2.1\"" - ]); - $rule = new CnesXMLIdentification($ibge_code); $this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); @@ -70,7 +30,7 @@ public function testSecondArgumentIsntPassed() public function testIdentificationErrorMessage() { $this->assertSame( - 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a 2.1', + 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.', $this->getErrorMessage(CnesXMLIdentification::class, ['0000', '2.1']) ); } diff --git a/tests/Unit/Rules/CnesXMLVersionXSDTest.php b/tests/Unit/Rules/CnesXMLVersionXSDTest.php new file mode 100644 index 0000000..4e1d162 --- /dev/null +++ b/tests/Unit/Rules/CnesXMLVersionXSDTest.php @@ -0,0 +1,46 @@ +mockXmlContents(['version_xsd' => "VERSION_XSD=\"$version\""]); + + $rule = new CnesXMLVersionXSD('2.1'); + + $this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); + } + + public function testNotPasses() + { + $version = '2'; + + $this->mockXmlContents(['version_xsd' => "VERSION_XSD=\"$version\""]); + + $rule = new CnesXMLVersionXSD('2.1'); + + $this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); + } + + public function testIdentificationErrorMessage() + { + $this->assertSame( + 'A versão do XML deve ser compatível com a 2.1', + $this->getErrorMessage(CnesXMLVersionXSD::class, ['2.1']) + ); + } +} diff --git a/tests/Unit/Rules/ZipHasValidCnesXMLTest.php b/tests/Unit/Rules/ZipHasValidCnesXMLTest.php index c591bd7..0ab6d0c 100644 --- a/tests/Unit/Rules/ZipHasValidCnesXMLTest.php +++ b/tests/Unit/Rules/ZipHasValidCnesXMLTest.php @@ -66,7 +66,29 @@ public function testFileWithInvalidIdentification() $this->assertFalse($passes); $this->assertEquals( - 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a 2.1', //phpcs:ignore + 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.', //phpcs:ignore + $validator->errors()->first('file') + ); + } + + public function testFileWithInvalidVersion() + { + $this->mockXmlContents([ + 'ibge_code' => '1234', + 'version_xsd' => 'VERSION_XSD="2"' + ]); + + $validator = Validator::make([ + 'file' => UploadedFile::fake()->create('xml.zip'), + ], [ + 'file' => [new ZipHasValidCnesXML('1234', null, '2.1')], + ]); + + $passes = $validator->passes(); + + $this->assertFalse($passes); + $this->assertEquals( + 'A versão do XML deve ser compatível com a 2.1', $validator->errors()->first('file') ); } @@ -75,14 +97,13 @@ public function testFileWithInvalidDate() { $this->mockXmlContents([ 'ibge_code' => '', - 'date' =>'2020-10-10', - 'version_xsd' => 'VERSION_XSD="2.1"' + 'date' =>'2020-10-10' ]); $validator = Validator::make([ 'file' => UploadedFile::fake()->create('xml.zip'), ], [ - 'file' => [new ZipHasValidCnesXML('', '2020-10-10', $version_xsd = '2.1')], + 'file' => [new ZipHasValidCnesXML('', '2020-10-10')], ]); $passes = $validator->passes(); @@ -94,6 +115,25 @@ public function testFileWithInvalidDate() ); } + public function testPassesWithoutVersionXSD() + { + $this->mockXmlContents([ + 'ibge_code' => '', + 'date' =>'2020-10-11', + 'version_xsd' => '' + ]); + + $validator = Validator::make([ + 'file' => UploadedFile::fake()->create('xml.zip'), + ], [ + 'file' => [new ZipHasValidCnesXML('', '2020-10-10')], + ]); + + $passes = $validator->passes(); + + $this->assertTrue($passes); + } + public function testValidFilePasses() { $this->mockXmlContents(['ibge_code' => '123456', 'date' => '2020-10-10']); From e39cd0e786ed1bd0fc7810c9c101ecc74dbeb444 Mon Sep 17 00:00:00 2001 From: Jedson Melo Date: Thu, 1 Apr 2021 11:35:19 -0300 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20altera=20vers=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d75cc7e..0b67ca7 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "version": "1.3.0", + "version": "1.3.1", "name": "sysvale/validation-rules", "description": "A Laravel library with useful custom rules", "type": "library",