| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 | <?php/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */namespace Magento\Integration\Test\Unit\Model\Config\Integration;/** * Test for validation rules implemented by XSD schema for API integration configuration. */class XsdTest extends \PHPUnit\Framework\TestCase{    /**     * @var string     */    protected $schemaFile;    protected function setUp()    {        if (!function_exists('libxml_set_external_entity_loader')) {            $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');        }        $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();        $this->schemaFile = $urnResolver->getRealPath(            'urn:magento:module:Magento_Integration:etc/integration/api.xsd'        );    }    /**     * @param string $fixtureXml     * @param array $expectedErrors     * @dataProvider exemplarXmlDataProvider     */    public function testExemplarXml($fixtureXml, array $expectedErrors)    {        $validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);        $validationStateMock->method('isValidationRequired')            ->willReturn(true);        $messageFormat = '%message%';        $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, $messageFormat);        $actualResult = $dom->validate($this->schemaFile, $actualErrors);        $this->assertEquals(empty($expectedErrors), $actualResult, "Validation result is invalid.");        $this->assertEquals($expectedErrors, $actualErrors, "Validation errors does not match.");    }    /**     * @return array     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)     */    public function exemplarXmlDataProvider()    {        return [            /** Valid configurations */            'valid' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                [],            ],            'valid with several entities' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                    <integration name="TestIntegration2">                        <resources>                            <resource name="Magento_Catalog::product_read" />                        </resources>                    </integration>                </integrations>',                [],            ],            /** Missing required nodes */            'empty root node' => [                '<integrations/>',                ["Element 'integrations': Missing child element(s). Expected is ( integration )."],            ],            'empty integration' => [                '<integrations>                    <integration name="TestIntegration" />                </integrations>',                ["Element 'integration': Missing child element(s). Expected is ( resources )."],            ],            'empty resources' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                        </resources>                    </integration>                </integrations>',                ["Element 'resources': Missing child element(s). Expected is ( resource )."],            ],            'irrelevant root node' => [                '<integration name="TestIntegration"/>',                ["Element 'integration': No matching global declaration available for the validation root."],            ],            /** Excessive nodes */            'irrelevant node in root' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                    <invalid/>                </integrations>',                ["Element 'invalid': This element is not expected. Expected is ( integration )."],            ],            'irrelevant node in integration' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                        <invalid/>                    </integration>                </integrations>',                ["Element 'invalid': This element is not expected."],            ],            'irrelevant node in resources' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        <invalid/>                        </resources>                    </integration>                </integrations>',                ["Element 'invalid': This element is not expected. Expected is ( resource )."],            ],            'irrelevant node in resource' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online">                                <invalid/>                            </resource>                        </resources>                    </integration>                </integrations>',                [                    "Element 'resource': Element content is not allowed, " .                    "because the content type is a simple type definition."                ],            ],            /** Excessive attributes */            'invalid attribute in root' => [                '<integrations invalid="invalid">                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                ["Element 'integrations', attribute 'invalid': The attribute 'invalid' is not allowed."],            ],            'invalid attribute in integration' => [                '<integrations>                    <integration name="TestIntegration1" invalid="invalid">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                ["Element 'integration', attribute 'invalid': The attribute 'invalid' is not allowed."],            ],            'invalid attribute in resources' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources invalid="invalid">                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                ["Element 'resources', attribute 'invalid': The attribute 'invalid' is not allowed."],            ],            'invalid attribute in resource' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" invalid="invalid" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                ["Element 'resource', attribute 'invalid': The attribute 'invalid' is not allowed."],            ],            /** Missing or empty required attributes */            'integration without name' => [                '<integrations>                    <integration>                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                ["Element 'integration': The attribute 'name' is required but missing."],            ],            'integration with empty name' => [                '<integrations>                    <integration name="">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="Magento_Customer::online" />                        </resources>                    </integration>                </integrations>',                [                    "Element 'integration', attribute 'name': [facet 'minLength'] The value '' has a length of '0'; " .                    "this underruns the allowed minimum length of '2'.",                    "Element 'integration', attribute 'name': " .                    "'' is not a valid value of the atomic type 'integrationNameType'."                ],            ],            'resource without name' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource />                        </resources>                    </integration>                </integrations>',                ["Element 'resource': The attribute 'name' is required but missing."],            ],            'resource with empty name' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::manage" />                            <resource name="" />                        </resources>                    </integration>                </integrations>',                [                    "Element 'resource', attribute 'name': [facet 'pattern'] " .                    "The value '' is not accepted by the pattern '.+_.+::.+'.",                    "Element 'resource', attribute 'name': '' " .                    "is not a valid value of the atomic type 'resourceNameType'."                ],            ],            /** Invalid values */            'resource with invalid name' => [                '<integrations>                    <integration name="TestIntegration1">                        <resources>                            <resource name="Magento_Customer::online" />                            <resource name="customer_manage" />                        </resources>                    </integration>                </integrations>',                [                    "Element 'resource', attribute 'name': [facet 'pattern'] " .                    "The value 'customer_manage' is not accepted by the pattern '.+_.+::.+'.",                    "Element 'resource', attribute 'name': 'customer_manage' " .                    "is not a valid value of the atomic type 'resourceNameType'."                ],            ]        ];    }}
 |