Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequestValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 class RequestValidatorTest extends \PHPUnit\Framework\TestCase
10 {
11  const SERVICE_METHOD = 'testMethod';
12 
13  const SERVICE_ID = 'Magento\Webapi\Controller\Rest\TestService';
14 
18  private $requestValidator;
19 
23  private $requestMock;
24 
26  private $storeManagerMock;
27 
29  private $storeMock;
30 
34  private $authorizationMock;
35 
39  private $routeMock;
40 
41  protected function setUp()
42  {
43  $this->requestMock = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
44  ->setMethods(
45  [
46  'isSecure',
47  'getRequestData',
48  'getParams',
49  'getParam',
50  'getRequestedServices',
51  'getPathInfo',
52  'getHttpHost',
53  'getMethod',
54  ]
55  )->disableOriginalConstructor()->getMock();
56  $this->requestMock->expects($this->any())
57  ->method('getHttpHost')
58  ->willReturn('testHostName.com');
59  $routerMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router::class)->setMethods(['match'])
60  ->disableOriginalConstructor()->getMock();
61  $this->routeMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router\Route::class)
62  ->setMethods(['isSecure', 'getServiceMethod', 'getServiceClass', 'getAclResources', 'getParameters'])
63  ->disableOriginalConstructor()->getMock();
64  $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\Webapi\Authorization::class)
65  ->disableOriginalConstructor()->getMock();
66  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
67  $this->storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
68  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
69  $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
70 
71  $this->requestValidator =
72  $objectManager->getObject(
73  \Magento\Webapi\Controller\Rest\RequestValidator::class,
74  [
75  'request' => $this->requestMock,
76  'router' => $routerMock,
77  'authorization' => $this->authorizationMock,
78  'storeManager' => $this->storeManagerMock
79  ]
80  );
81 
82  // Set default expectations used by all tests
83  $this->routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID));
84  $this->routeMock->expects($this->any())->method('getServiceMethod')
85  ->will($this->returnValue(self::SERVICE_METHOD));
86  $routerMock->expects($this->any())->method('match')->will($this->returnValue($this->routeMock));
87 
88  parent::setUp();
89  }
90 
96  public function testSecureRouteAndRequest($isSecureRoute, $isSecureRequest)
97  {
98  $this->routeMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRoute));
99  $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1']));
100  $this->requestMock->expects($this->any())->method('getRequestData')->will($this->returnValue([]));
101  $this->requestMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRequest));
102  $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
103  $this->requestValidator->validate();
104  }
105 
112  {
113  // Each array contains return type for isSecure method of route and request objects.
114  return [[true, true], [false, true], [false, false]];
115  }
116 
124  {
125  $this->routeMock->expects($this->any())->method('isSecure')->will($this->returnValue(true));
126  $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1']));
127  $this->requestMock->expects($this->any())->method('isSecure')->will($this->returnValue(false));
128  $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
129 
130  $this->requestValidator->validate();
131  }
132 
137  public function testAuthorizationFailed()
138  {
139  $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(false));
140  $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['5', '6']));
141  $this->requestValidator->validate();
142  }
143 }
$objectManager
Definition: bootstrap.php:17