Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OauthUserContextTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class OauthUserContextTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $objectManager;
20 
24  protected $oauthUserContext;
25 
29  protected $request;
30 
35 
40 
44  protected $oauthService;
45 
46  protected function setUp()
47  {
48  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
49 
50  $this->request = $this->getMockBuilder(\Magento\Framework\Webapi\Request::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['getConsumerId'])
53  ->getMock();
54 
55  $this->integrationService = $this->getMockBuilder(\Magento\Integration\Api\IntegrationServiceInterface::class)
56  ->disableOriginalConstructor()
57  ->setMethods(
58  [
59  'findByName',
60  'update',
61  'create',
62  'get',
63  'findByConsumerId',
64  'findActiveIntegrationByConsumerId',
65  'delete',
66  'getSelectedResources'
67  ]
68  )
69  ->getMock();
70 
71  $this->oauthRequestHelper = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Request::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['prepareRequest', 'getRequestUrl'])
74  ->getMock();
75 
76  $this->oauthService = $this->getMockBuilder(\Magento\Framework\Oauth\Oauth::class)
77  ->disableOriginalConstructor()
78  ->setMethods(['validateAccessTokenRequest'])
79  ->getMock();
80 
81  $this->oauthUserContext = $this->objectManager->getObject(
82  \Magento\Webapi\Model\Authorization\OauthUserContext::class,
83  [
84  'request' => $this->request,
85  'integrationService' => $this->integrationService,
86  'oauthService' => $this->oauthService,
87  'oauthHelper' => $this->oauthRequestHelper
88  ]
89  );
90  }
91 
92  public function testGetUserType()
93  {
94  $this->assertEquals(UserContextInterface::USER_TYPE_INTEGRATION, $this->oauthUserContext->getUserType());
95  }
96 
97  public function testGetUserIdExist()
98  {
99  $integrationId = 12345;
100 
101  $this->setupUserId($integrationId, ['oauth_token' => 'asdcfsdvanskdcalkdsjcfljldk']);
102 
103  $this->assertEquals($integrationId, $this->oauthUserContext->getUserId());
104  }
105 
106  public function testGetUserIdDoesNotExist()
107  {
108  $integrationId = null;
109 
110  $this->setupUserId($integrationId, ['oauth_token' => 'asdcfsdvanskdcalkdsjcfljldk']);
111 
112  $this->assertEquals($integrationId, $this->oauthUserContext->getUserId());
113  }
114 
116  {
117  $integrationId = 12345;
118 
119  $this->setupUserId($integrationId, []);
120 
121  $this->assertEquals(null, $this->oauthUserContext->getUserId());
122  }
123 
129  public function setupUserId($integrationId, $oauthRequest)
130  {
131  $integration = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
132  ->disableOriginalConstructor()
133  ->setMethods(['getId', '__wakeup'])
134  ->getMock();
135 
136  $this->integrationService->expects($this->any())
137  ->method('findActiveIntegrationByConsumerId')
138  ->will($this->returnValue($integration));
139 
140  $this->oauthRequestHelper->expects($this->once())
141  ->method('prepareRequest')
142  ->will($this->returnValue($oauthRequest));
143 
144  $this->oauthService->expects($this->any())
145  ->method('validateAccessTokenRequest')
146  ->will($this->returnValue(1));
147 
148  $integration->expects($this->any())
149  ->method('getId')
150  ->will($this->returnValue($integrationId));
151  }
152 }