/** * @param object $methodInstance * @param bool $isAllowed * @param bool $isAvailable * @dataProvider restrictAdminBillingAgreementUsageDataProvider */ public function testExecute($methodInstance, $isAllowed, $isAvailable) { $this->_event->setMethodInstance($methodInstance); $this->_authorization->expects($this->any())->method('isAllowed')->with('Magento_Paypal::use')->will($this->returnValue($isAllowed)); $result = new \stdClass(); $result->isAvailable = true; $this->_event->setResult($result); $this->_model->execute($this->_observer); $this->assertEquals($isAvailable, $result->isAvailable); }
public function testFilterAclElements() { $scheduledStructureMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout\\ScheduledStructure')->disableOriginalConstructor()->getMock(); $structureMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout\\Data\\Structure')->disableOriginalConstructor()->getMock(); $elements = ['element_0' => [0 => '', 1 => ['attributes' => ['name' => 'element_0']]], 'element_1' => [0 => '', 1 => ['attributes' => ['name' => 'element_1', 'acl' => 'acl_authorised']]], 'element_2' => [0 => '', 1 => ['attributes' => ['name' => 'element_2', 'acl' => 'acl_non_authorised']]], 'element_3' => [0 => '', 1 => ['attributes' => ['name' => 'element_3', 'acl' => 'acl_non_authorised']]]]; $scheduledStructureMock->expects($this->once())->method('getElements')->willReturn($elements); $this->authorizationMock->expects($this->exactly(3))->method('isAllowed')->willReturnMap([['acl_authorised', null, true], ['acl_non_authorised', null, false]]); $structureMock->expects($this->exactly(3))->method('getChildren')->willReturnMap([['element_2', ['element_2_child' => []]], ['element_2_child', []], ['element_3', []]]); $scheduledStructureMock->expects($this->exactly(3))->method('unsetElement')->willReturnMap([['element_2', null], ['element_2_child', null], ['element_3', null]]); $structureMock->expects($this->exactly(2))->method('unsetElement')->willReturnMap([['element_2', true, true], ['element_3', true, true]]); $this->model->filterAclElements($scheduledStructureMock, $structureMock); }
public function testGetMethodAllStoresInvalid() { $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1'])); $this->_authorizationMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); $this->storeMock->expects($this->once())->method('getCode')->willReturn('admin'); $this->_requestMock->expects($this->once())->method('getMethod')->willReturn('get'); $this->_restController->dispatch($this->_requestMock); $this->assertTrue($this->_responseMock->isException()); $this->assertSame("Cannot perform GET operation with store code 'all'", $this->_responseMock->getException()[0]->getMessage()); }
/** * @param bool $isPermissionAllowed * @param array $expectedValue * @dataProvider buildOutputDataArrayWithPermissionProvider */ public function testBuildOutputDataArrayWithPermission($isPermissionAllowed, $expectedValue) { $dataObject = new \Magento\Framework\Reflection\Test\Unit\ExtensionAttributesObject(); $dataObjectType = 'Magento\\Framework\\Reflection\\Test\\Unit\\ExtensionAttributesObject'; $methodName = 'getAttrName'; $attributeName = 'attr_name'; $attributeValue = 'attrName'; $this->methodsMapProcessorMock->expects($this->once())->method('getMethodsMap')->with($dataObjectType)->will($this->returnValue([$methodName => []])); $this->methodsMapProcessorMock->expects($this->once())->method('isMethodValidForDataField')->with($dataObjectType, $methodName)->will($this->returnValue(true)); $this->fieldNamerMock->expects($this->once())->method('getFieldNameForMethodName')->with($methodName)->will($this->returnValue($attributeName)); $permissionName = 'Magento_Permission'; $this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue([$dataObjectType => [$attributeName => [Converter::RESOURCE_PERMISSIONS => [$permissionName]]]])); $this->authorizationMock->expects($this->once())->method('isAllowed')->with($permissionName)->will($this->returnValue($isPermissionAllowed)); if ($isPermissionAllowed) { $this->methodsMapProcessorMock->expects($this->once())->method('getMethodReturnType')->with($dataObjectType, $methodName)->will($this->returnValue('string')); $this->typeCasterMock->expects($this->once())->method('castValueToType')->with($attributeValue, 'string')->will($this->returnValue($attributeValue)); } $value = $this->model->buildOutputDataArray($dataObject, $dataObjectType); $this->assertEquals($value, $expectedValue); }
/** * @param array $requestData Data from the request * @param array $parameters Data from config about which parameters to override * @param array $expectedOverriddenParams Result of overriding $requestData when applying rules from $parameters * @param int $userId The id of the user invoking the request * @param int $userType The type of user invoking the request * * @dataProvider overrideParmasDataProvider */ public function testOverrideParams($requestData, $parameters, $expectedOverriddenParams, $userId, $userType) { $this->_routeMock->expects($this->once())->method('getParameters')->will($this->returnValue($parameters)); $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1'])); $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true)); $this->_requestMock->expects($this->any())->method('getRequestData')->will($this->returnValue($requestData)); $this->userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId)); $this->userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType)); // serializer should expect overridden params $this->serializerMock->expects($this->once())->method('getInputData')->with($this->equalTo('Magento\\Webapi\\Controller\\TestService'), $this->equalTo('testMethod'), $this->equalTo($expectedOverriddenParams)); $this->_restController->dispatch($this->_requestMock); }
public function testAuthorizationFailed() { $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(false)); $this->_oauthServiceMock->expects($this->any())->method('validateAccessTokenRequest')->will($this->returnValue('fred')); $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['5', '6'])); $this->_restController->dispatch($this->_requestMock); /** Ensure that response contains proper error message. */ $expectedMsg = 'Consumer is not authorized to access 5, 6'; AuthorizationException::NOT_AUTHORIZED; $this->assertTrue($this->_responseMock->isException()); $exceptionArray = $this->_responseMock->getException(); $this->assertEquals($expectedMsg, $exceptionArray[0]->getMessage()); }
/** * @covers \Magento\Cms\Model\Wysiwyg\Config::getConfig * @param array $data * @param boolean $isAuthorizationAllowed * @param array $expectedResults * * @dataProvider getConfigDataProvider */ public function testGetConfig($data, $isAuthorizationAllowed, $expectedResults) { $wysiwygPluginSettings = ['wysiwygPluginSettings' => 'wysiwyg is here']; $pluginSettings = ['pluginSettings' => 'plugins are here']; $this->backendUrlMock->expects($this->atLeastOnce())->method('getUrl')->withConsecutive(['cms/wysiwyg/directive'], ['cms/wysiwyg_images/index']); $this->assetRepoMock->expects($this->atLeastOnce())->method('getUrl')->withConsecutive(['mage/adminhtml/wysiwyg/tiny_mce/themes/advanced/skins/default/dialog.css'], ['mage/adminhtml/wysiwyg/tiny_mce/themes/advanced/skins/default/content.css']); $this->authorizationMock->expects($this->atLeastOnce())->method('isAllowed')->with('Magento_Cms::media_gallery')->willReturn($isAuthorizationAllowed); $this->variableConfigMock->expects($this->any())->method('getWysiwygPluginSettings')->willReturn($wysiwygPluginSettings); $this->widgetConfigMock->expects($this->any())->method('getPluginSettings')->willReturn($pluginSettings); $config = $this->wysiwygConfig->getConfig($data); $this->assertInstanceOf('Magento\\Framework\\Object', $config); $this->assertEquals($expectedResults[0], $config->getData('someData')); $this->assertEquals($expectedResults[1], $config->getData('wysiwygPluginSettings')); $this->assertEquals($expectedResults[2], $config->getData('pluginSettings')); }
/** * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithNewCustomerAndException() { $subscription = 'false'; $postValue = ['customer' => ['coolness' => false, 'disable_auto_group_change' => 'false'], 'subscription' => $subscription]; $filteredData = ['coolness' => false, 'disable_auto_group_change' => 'false']; /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */ $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock(); $attributeMock->expects($this->once())->method('getAttributeCode')->willReturn('coolness'); $attributeMock->expects($this->once())->method('getFrontendInput')->willReturn('int'); $attributes = [$attributeMock]; $this->requestMock->expects($this->exactly(2))->method('getPostValue')->willReturn($postValue); $this->requestMock->expects($this->exactly(2))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, null]]); /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */ $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock(); $this->formFactoryMock->expects($this->once())->method('create')->with(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE)->willReturn($formMock); $formMock->expects($this->once())->method('extractData')->with($this->requestMock, 'customer')->willReturn($filteredData); /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */ $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $this->objectFactoryMock->expects($this->once())->method('create')->with(['data' => $postValue])->willReturn($objectMock); $objectMock->expects($this->once())->method('getData')->with('customer')->willReturn($postValue['customer']); $formMock->expects($this->once())->method('getAttributes')->willReturn($attributes); /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */ $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock); $exception = new \Exception(__('Exception')); $this->managementMock->expects($this->once())->method('createAccount')->with($customerMock, null, '')->willThrowException($exception); $customerMock->expects($this->never())->method('getId'); $this->authorizationMock->expects($this->never())->method('isAllowed'); $this->subscriberFactoryMock->expects($this->never())->method('create'); $this->sessionMock->expects($this->never())->method('unsCustomerData'); $this->registryMock->expects($this->never())->method('register'); $this->messageManagerMock->expects($this->never())->method('addSuccess'); $this->messageManagerMock->expects($this->once())->method('addException')->with($exception, __('Something went wrong while saving the customer.')); $this->sessionMock->expects($this->once())->method('setCustomerData')->with($postValue); /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */ $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $this->redirectFactoryMock->expects($this->once())->method('create')->with([])->willReturn($redirectMock); $redirectMock->expects($this->once())->method('setPath')->with('customer/*/new', ['_current' => true])->willReturn(true); $this->assertEquals($redirectMock, $this->model->execute()); }
/** * @covers \Magento\Cms\Model\Wysiwyg\Config::getConfig * @param array $data * @param boolean $isAuthorizationAllowed * @param array $expectedResults * * @dataProvider getConfigDataProvider */ public function testGetConfig($data, $isAuthorizationAllowed, $expectedResults) { $wysiwygPluginSettings = ['wysiwygPluginSettings' => 'wysiwyg is here']; $pluginSettings = ['pluginSettings' => 'plugins are here']; $this->backendUrlMock->expects($this->atLeastOnce())->method('getUrl')->withConsecutive(['cms/wysiwyg/directive'], ['cms/wysiwyg_images/index']); $this->backendUrlMock->expects($this->once())->method('getBaseUrl')->willReturn('localhost/index.php/'); $this->assetRepoMock->expects($this->atLeastOnce())->method('getUrl')->withConsecutive(['mage/adminhtml/wysiwyg/tiny_mce/themes/advanced/skins/default/dialog.css'], ['mage/adminhtml/wysiwyg/tiny_mce/themes/advanced/skins/default/content.css']); $this->filesystemMock->expects($this->once())->method('getUri')->willReturn('pub/static'); /** @var \Magento\Framework\View\Asset\ContextInterface|\PHPUnit_Framework_MockObject_MockObject $contextMock */ $contextMock = $this->getMock(\Magento\Framework\View\Asset\ContextInterface::class); $contextMock->expects($this->once())->method('getBaseUrl')->willReturn('localhost/pub/static/'); $this->assetRepoMock->expects($this->once())->method('getStaticViewFileContext')->willReturn($contextMock); $this->authorizationMock->expects($this->atLeastOnce())->method('isAllowed')->with('Magento_Cms::media_gallery')->willReturn($isAuthorizationAllowed); $this->variableConfigMock->expects($this->any())->method('getWysiwygPluginSettings')->willReturn($wysiwygPluginSettings); $this->widgetConfigMock->expects($this->any())->method('getPluginSettings')->willReturn($pluginSettings); $config = $this->wysiwygConfig->getConfig($data); $this->assertInstanceOf('Magento\\Framework\\DataObject', $config); $this->assertEquals($expectedResults[0], $config->getData('someData')); $this->assertEquals($expectedResults[1], $config->getData('wysiwygPluginSettings')); $this->assertEquals($expectedResults[2], $config->getData('pluginSettings')); $this->assertEquals('localhost/pub/static/', $config->getData('baseStaticUrl')); $this->assertEquals('localhost/pub/static/', $config->getData('baseStaticDefaultUrl')); }
public function testGetAfterElementHtml() { $model = $this->objectManager->getObject('Magento\\Catalog\\Block\\Adminhtml\\Product\\Helper\\Form\\Category', ['authorization' => $this->authorization]); $this->authorization->expects($this->any())->method('isAllowed')->will($this->returnValue(false)); $this->assertEmpty($model->getAfterElementHtml()); }
/** * @param bool $isAllowed * @param null|bool $url * @dataProvider permissionProvider */ public function testGetUrl($isAllowed, $url) { $this->authorizationMock->expects($this->once())->method('isAllowed')->with('Magento_Sales::sales_invoice', null)->willReturn($isAllowed); $this->assertEquals($url, $this->urlGenerator->getUrl(new \Magento\Framework\DataObject())); }
public function testModifyMeta() { $this->registryMock->expects($this->once())->method('registry')->with('use_wrapper')->willReturn(true); $this->authorizationMock->expects($this->once())->method('isAllowed')->with('Magento_Catalog::attributes_attributes')->willReturn(true); $this->assertArrayHasKey('add_attribute_modal', $this->getModel()->modifyMeta([])); }