Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AclTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AclTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_model;
14 
18  protected $_aclMock;
19 
23  protected $_aclBuilderMock;
24 
25  protected function setUp()
26  {
27  $this->_aclMock = $this->createMock(\Magento\Framework\Acl::class);
28  $this->_aclBuilderMock = $this->createMock(\Magento\Framework\Acl\Builder::class);
29  $this->_aclBuilderMock->expects($this->any())->method('getAcl')->will($this->returnValue($this->_aclMock));
30  $this->_model = new \Magento\Framework\Authorization\Policy\Acl($this->_aclBuilderMock);
31  }
32 
34  {
35  $this->_aclMock->expects(
36  $this->once()
37  )->method(
38  'isAllowed'
39  )->with(
40  'some_role',
41  'some_resource'
42  )->will(
43  $this->returnValue(true)
44  );
45 
46  $this->assertTrue($this->_model->isAllowed('some_role', 'some_resource'));
47  }
48 
50  {
51  $this->_aclMock->expects(
52  $this->once()
53  )->method(
54  'isAllowed'
55  )->with(
56  'some_role',
57  'some_resource'
58  )->will(
59  $this->throwException(new \Zend_Acl_Role_Registry_Exception())
60  );
61 
62  $this->_aclMock->expects($this->once())->method('has')->with('some_resource')->will($this->returnValue(true));
63 
64  $this->assertFalse($this->_model->isAllowed('some_role', 'some_resource'));
65  }
66 
68  {
69  $this->_aclMock->expects(
70  $this->at(0)
71  )->method(
72  'isAllowed'
73  )->with(
74  'some_role',
75  'some_resource'
76  )->will(
77  $this->throwException(new \Zend_Acl_Role_Registry_Exception())
78  );
79 
80  $this->_aclMock->expects($this->once())->method('has')->with('some_resource')->will($this->returnValue(false));
81 
82  $this->_aclMock->expects(
83  $this->at(2)
84  )->method(
85  'isAllowed'
86  )->with(
87  'some_role',
88  null
89  )->will(
90  $this->returnValue(true)
91  );
92 
93  $this->assertTrue($this->_model->isAllowed('some_role', 'some_resource'));
94  }
95 }