_markTestAsSoapOnly("WSDL generation tests are intended to be executed for SOAP adapter only."); $this->_storeCode = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class) ->getStore()->getCode(); parent::setUp(); } public function testMultiServiceWsdl() { $this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}" . "?services=testModule5AllSoapAndRestV1%2CtestModule5AllSoapAndRestV2"; $wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV1,testModule5AllSoapAndRestV2'; $wsdlContent = $this->_convertXmlToString($this->_getWsdlContent($wsdlUrl)); $this->isSingleService = false; $this->_checkTypesDeclaration($wsdlContent); $this->_checkPortTypeDeclaration($wsdlContent); $this->_checkBindingDeclaration($wsdlContent); $this->_checkServiceDeclaration($wsdlContent); $this->_checkMessagesDeclaration($wsdlContent); $this->_checkFaultsDeclaration($wsdlContent); } public function testSingleServiceWsdl() { $this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}?services=testModule5AllSoapAndRestV2"; $wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV2'; $wsdlContent = $this->_convertXmlToString($this->_getWsdlContent($wsdlUrl)); $this->isSingleService = true; $this->_checkTypesDeclaration($wsdlContent); $this->_checkPortTypeDeclaration($wsdlContent); $this->_checkBindingDeclaration($wsdlContent); $this->_checkServiceDeclaration($wsdlContent); $this->_checkMessagesDeclaration($wsdlContent); $this->_checkFaultsDeclaration($wsdlContent); } public function testNoAuthorizedServices() { $wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV2'; $connection = curl_init($wsdlUrl); curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); $responseContent = curl_exec($connection); $this->assertEquals(curl_getinfo($connection, CURLINFO_HTTP_CODE), 401); $this->assertContains("The consumer isn't authorized to access %resources.", $responseContent); } public function testInvalidWsdlUrlNoServices() { $responseContent = $this->_getWsdlContent($this->_getBaseWsdlUrl()); $this->assertContains("Requested services are missing.", $responseContent); } public function testInvalidWsdlUrlInvalidParameter() { $wsdlUrl = $this->_getBaseWsdlUrl() . '&invalid'; $responseContent = $this->_getWsdlContent($wsdlUrl); $this->assertContains("Not allowed parameters", $responseContent); } /** * Remove unnecessary spaces and line breaks from xml string. * * @param string $xml * @return string */ protected function _convertXmlToString($xml) { return str_replace([' ', "\n", "\r", " ", " "], '', $xml); } /** * Retrieve WSDL content. * * @param string $wsdlUrl * @return string|boolean */ protected function _getWsdlContent($wsdlUrl) { $accessCredentials = \Magento\TestFramework\Authentication\OauthHelper::getApiAccessCredentials()['key']; $connection = curl_init($wsdlUrl); curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($connection, CURLOPT_HTTPHEADER, ['header' => "Authorization: Bearer " . $accessCredentials]); $responseContent = curl_exec($connection); $responseDom = new \DOMDocument(); $this->assertTrue( $responseDom->loadXML($responseContent), "Valid XML is always expected as a response for WSDL request." ); return $responseContent; } /** * Generate base WSDL URL (without any services specified) * * @return string */ protected function _getBaseWsdlUrl() { /** @var \Magento\TestFramework\TestCase\Webapi\Adapter\Soap $soapAdapter */ $soapAdapter = $this->_getWebApiAdapter(self::ADAPTER_SOAP); $wsdlUrl = $soapAdapter->generateWsdlUrl([]); return $wsdlUrl; } /** * Ensure that types section has correct structure. * * @param string $wsdlContent */ protected function _checkTypesDeclaration($wsdlContent) { // @codingStandardsIgnoreStart $typesSectionDeclaration = <<< TYPES_SECTION_DECLARATION TYPES_SECTION_DECLARATION; // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($typesSectionDeclaration), $wsdlContent, 'Types section declaration is invalid' ); $this->_checkElementsDeclaration($wsdlContent); $this->_checkComplexTypesDeclaration($wsdlContent); } /** * @param string $wsdlContent */ protected function _checkElementsDeclaration($wsdlContent) { if ($this->isSingleService) { $requestElement = <<< REQUEST_ELEMENT REQUEST_ELEMENT; } else { $requestElement = <<< REQUEST_ELEMENT REQUEST_ELEMENT; } $this->assertContains( $this->_convertXmlToString($requestElement), $wsdlContent, 'Request element declaration in types section is invalid' ); if ($this->isSingleService) { $responseElement = <<< RESPONSE_ELEMENT RESPONSE_ELEMENT; } else { $responseElement = <<< RESPONSE_ELEMENT RESPONSE_ELEMENT; } $this->assertContains( $this->_convertXmlToString($responseElement), $wsdlContent, 'Response element declaration in types section is invalid' ); } /** * @param string $wsdlContent * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _checkComplexTypesDeclaration($wsdlContent) { // @codingStandardsIgnoreStart if ($this->isSingleService) { $requestType = <<< REQUEST_TYPE Retrieve existing item. testModule5AllSoapAndRestV2Item Yes REQUEST_TYPE; } else { $requestType = <<< REQUEST_TYPE Retrieve an item. testModule5AllSoapAndRestV1Item Yes REQUEST_TYPE; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($requestType), $wsdlContent, 'Request type declaration in types section is invalid' ); // @codingStandardsIgnoreStart if ($this->isSingleService) { $responseType = <<< RESPONSE_TYPE Response container for the testModule5AllSoapAndRestV2Item call. testModule5AllSoapAndRestV2Item Always RESPONSE_TYPE; } else { $responseType = <<< RESPONSE_TYPE Response container for the testModule5AllSoapAndRestV1Item call. testModule5AllSoapAndRestV1Item Always RESPONSE_TYPE; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($responseType), $wsdlContent, 'Response type declaration in types section is invalid' ); $this->_checkReferencedTypeDeclaration($wsdlContent); } /** * Ensure that complex type generated from Data Object is correct. * * @param string $wsdlContent * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _checkReferencedTypeDeclaration($wsdlContent) { // @codingStandardsIgnoreStart if ($this->isSingleService) { $referencedType = <<< RESPONSE_TYPE Some Data Object short description. Data Object long multi line description. testModule5AllSoapAndRestV2Item testModule5AllSoapAndRestV2Create testModule5AllSoapAndRestV2Update testModule5AllSoapAndRestV2Delete Always testModule5AllSoapAndRestV2Create testModule5AllSoapAndRestV2Update Yes RESPONSE_TYPE; } else { $referencedType = <<< RESPONSE_TYPE Some Data Object short description. Data Object long multi line description. Item ID testModule5AllSoapAndRestV1Item testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Always testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Yes Item name testModule5AllSoapAndRestV1Item testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Conditionally testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate No If entity is enabled false testModule5AllSoapAndRestV1Item testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Conditionally testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate No If current entity has a property defined false testModule5AllSoapAndRestV1Item testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Conditionally testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate No Custom attributes values. array testModule5AllSoapAndRestV1Item testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate Conditionally testModule5AllSoapAndRestV1Create testModule5AllSoapAndRestV1Update testModule5AllSoapAndRestV1NestedUpdate No RESPONSE_TYPE; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($referencedType), $wsdlContent, 'Declaration of complex type generated from Data Object, which is referenced in response, is invalid' ); } /** * Ensure that port type sections have correct structure. * * @param string $wsdlContent */ protected function _checkPortTypeDeclaration($wsdlContent) { if ($this->isSingleService) { $firstPortType = <<< FIRST_PORT_TYPE FIRST_PORT_TYPE; } else { $firstPortType = <<< FIRST_PORT_TYPE FIRST_PORT_TYPE; } $this->assertContains( $this->_convertXmlToString($firstPortType), $wsdlContent, 'Port type declaration is missing or invalid' ); if (!$this->isSingleService) { $secondPortType = <<< SECOND_PORT_TYPE SECOND_PORT_TYPE; $this->assertContains( $this->_convertXmlToString($secondPortType), $wsdlContent, 'Port type declaration is missing or invalid' ); } if ($this->isSingleService) { $operationDeclaration = <<< OPERATION_DECLARATION OPERATION_DECLARATION; } else { $operationDeclaration = <<< OPERATION_DECLARATION OPERATION_DECLARATION; } $this->assertContains( $this->_convertXmlToString($operationDeclaration), $wsdlContent, 'Operation in port type is invalid' ); } /** * Ensure that binding sections have correct structure. * * @param string $wsdlContent */ protected function _checkBindingDeclaration($wsdlContent) { if ($this->isSingleService) { $firstBinding = <<< FIRST_BINDING FIRST_BINDING; } else { $firstBinding = <<< FIRST_BINDING FIRST_BINDING; } $this->assertContains( $this->_convertXmlToString($firstBinding), $wsdlContent, 'Binding declaration is missing or invalid' ); if (!$this->isSingleService) { $secondBinding = <<< SECOND_BINDING SECOND_BINDING; $this->assertContains( $this->_convertXmlToString($secondBinding), $wsdlContent, 'Binding declaration is missing or invalid' ); } if ($this->isSingleService) { $operationDeclaration = <<< OPERATION_DECLARATION OPERATION_DECLARATION; } else { $operationDeclaration = <<< OPERATION_DECLARATION OPERATION_DECLARATION; } $this->assertContains( $this->_convertXmlToString($operationDeclaration), $wsdlContent, 'Operation in binding is invalid' ); } /** * Ensure that service sections have correct structure. * * @param string $wsdlContent */ protected function _checkServiceDeclaration($wsdlContent) { // @codingStandardsIgnoreStart if ($this->isSingleService) { $firstServiceDeclaration = <<< FIRST_SERVICE_DECLARATION FIRST_SERVICE_DECLARATION; } else { $firstServiceDeclaration = <<< FIRST_SERVICE_DECLARATION FIRST_SERVICE_DECLARATION; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($firstServiceDeclaration), $wsdlContent, 'First service section is invalid' ); // @codingStandardsIgnoreStart if (!$this->isSingleService) { $secondServiceDeclaration = <<< SECOND_SERVICE_DECLARATION SECOND_SERVICE_DECLARATION; // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($secondServiceDeclaration), $wsdlContent, 'Second service section is invalid' ); } } /** * Ensure that messages sections have correct structure. * * @param string $wsdlContent */ protected function _checkMessagesDeclaration($wsdlContent) { $itemMessagesDeclaration = <<< MESSAGES_DECLARATION MESSAGES_DECLARATION; $this->assertContains( $this->_convertXmlToString($itemMessagesDeclaration), $wsdlContent, 'Messages section for "item" operation is invalid' ); $itemsMessagesDeclaration = <<< MESSAGES_DECLARATION MESSAGES_DECLARATION; $this->assertContains( $this->_convertXmlToString($itemsMessagesDeclaration), $wsdlContent, 'Messages section for "items" operation is invalid' ); } /** * Ensure that SOAP faults are declared properly. * * @param string $wsdlContent */ protected function _checkFaultsDeclaration($wsdlContent) { $this->_checkFaultsPortTypeSection($wsdlContent); $this->_checkFaultsBindingSection($wsdlContent); $this->_checkFaultsMessagesSection($wsdlContent); $this->_checkFaultsComplexTypeSection($wsdlContent); } /** * @param string $wsdlContent */ protected function _checkFaultsPortTypeSection($wsdlContent) { $faultsInPortType = <<< FAULT_IN_PORT_TYPE FAULT_IN_PORT_TYPE; $this->assertContains( $this->_convertXmlToString($faultsInPortType), $wsdlContent, 'SOAP Fault section in port type section is invalid' ); } /** * @param string $wsdlContent */ protected function _checkFaultsBindingSection($wsdlContent) { $faultsInBinding = <<< FAULT_IN_BINDING FAULT_IN_BINDING; $this->assertContains( $this->_convertXmlToString($faultsInBinding), $wsdlContent, 'SOAP Fault section in binding section is invalid' ); } /** * @param string $wsdlContent */ protected function _checkFaultsMessagesSection($wsdlContent) { $genericFaultMessage = <<< GENERIC_FAULT_IN_MESSAGES GENERIC_FAULT_IN_MESSAGES; $this->assertContains( $this->_convertXmlToString($genericFaultMessage), $wsdlContent, 'Generic SOAP Fault declaration in messages section is invalid' ); } /** * @param string $wsdlContent * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _checkFaultsComplexTypeSection($wsdlContent) { $this->assertContains( $this->_convertXmlToString(''), $wsdlContent, 'Default SOAP Fault complex type element declaration is invalid' ); // @codingStandardsIgnoreStart $genericFaultType = <<< GENERIC_FAULT_COMPLEX_TYPE Exception calls stack trace. Additional exception parameters. array Additional wrapped errors. array GENERIC_FAULT_COMPLEX_TYPE; $this->assertContains( $this->_convertXmlToString($genericFaultType), $wsdlContent, 'Default SOAP Fault complex types declaration is invalid' ); $detailsParameterType = <<< PARAM_COMPLEX_TYPE PARAM_COMPLEX_TYPE; $this->assertContains( $this->_convertXmlToString($detailsParameterType), $wsdlContent, 'Details parameter complex types declaration is invalid.' ); // @codingStandardsIgnoreStart if ($this->isSingleService) { $detailsWrappedErrorType = <<< WRAPPED_ERROR_COMPLEX_TYPE Message parameters. array WRAPPED_ERROR_COMPLEX_TYPE; } else { $detailsWrappedErrorType = <<< WRAPPED_ERROR_COMPLEX_TYPE Message parameters. array WRAPPED_ERROR_COMPLEX_TYPE; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($detailsWrappedErrorType), $wsdlContent, 'Details wrapped error complex types declaration is invalid.' ); $detailsParametersType = <<< PARAMETERS_COMPLEX_TYPE An array of GenericFaultParameter items. An item of ArrayOfGenericFaultParameter. PARAMETERS_COMPLEX_TYPE; // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($detailsParametersType), $wsdlContent, 'Details parameters (array of parameters) complex types declaration is invalid.' ); if ($this->isSingleService) { // @codingStandardsIgnoreStart $detailsWrappedErrorsType = <<< WRAPPED_ERRORS_COMPLEX_TYPE An array of WrappedError items. An item of ArrayOfWrappedError. WRAPPED_ERRORS_COMPLEX_TYPE; } else { $detailsWrappedErrorsType = <<< WRAPPED_ERRORS_COMPLEX_TYPE An array of WrappedError items. An item of ArrayOfWrappedError. WRAPPED_ERRORS_COMPLEX_TYPE; } // @codingStandardsIgnoreEnd $this->assertContains( $this->_convertXmlToString($detailsWrappedErrorsType), $wsdlContent, 'Details wrapped errors (array of wrapped errors) complex types declaration is invalid.' ); } }