AuthLink = $this->getMockBuilder(AuthLinkHelper::class) ->setMethods(['isAuthorized']) ->setConstructorArgs([$view]) ->getMock(); } /** * tearDown method * * @return void */ public function tearDown(): void { unset($this->AuthLink); parent::tearDown(); } /** * Test link * * @return void */ public function testLinkFalseWithMock(): void { $this->AuthLink->expects($this->once()) ->method('isAuthorized') ->with( $this->equalTo(['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile']) ) ->will($this->returnValue(false)); $result = $this->AuthLink->link( 'title', ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile'], ['before' => 'before_', 'after' => '_after', 'class' => 'link-class'] ); $this->assertEmpty($result); } /** * Test link * * @return void */ public function testLinkAuthorizedHappy(): void { Router::connect('/profile', [ 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile', ]); $this->AuthLink->expects($this->once()) ->method('isAuthorized') ->with( $this->equalTo(['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile']) ) ->will($this->returnValue(true)); $link = $this->AuthLink->link( 'title', ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile'], ['before' => 'before_', 'after' => '_after', 'class' => 'link-class'] ); $this->assertSame('before_title_after', $link); } /** * Test link * * @return void */ public function testLinkAuthorizedAllowedTrue(): void { $link = $this->AuthLink->link('title', '/', ['allowed' => true, 'before' => 'before_', 'after' => '_after', 'class' => 'link-class']); $this->assertSame('before_title_after', $link); } /** * Test link * * @return void */ public function testLinkAuthorizedAllowedFalse(): void { $link = $this->AuthLink->link('title', '/', ['allowed' => false, 'before' => 'before_', 'after' => '_after', 'class' => 'link-class']); $this->assertEmpty($link); } /** * Test getRequest method * * @retunr void */ public function testGetRequest(): void { $actual = $this->AuthLink->getRequest(); $this->assertInstanceOf(ServerRequest::class, $actual); } /** * Test post link with delete user method * Logged as Super user * * @return void */ public function testPostLinkAuthorizedAllowedTrueLoggedAsAdmin(): void { $url = [ 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'delete', '00000000-0000-0000-0000-000000000010', ]; Router::connect('/Users/delete/00000000-0000-0000-0000-000000000010', $url); $this->AuthLink->expects($this->once()) ->method('isAuthorized') ->with( $this->equalTo($url) ) ->will($this->returnValue(true)); $link = $this->AuthLink->postLink('Post Link Title', $url, [ 'allowed' => true, 'class' => 'link-class', 'confirm' => 'confirmation message', ]); $this->assertStringContainsString('data-confirm-message="confirmation message"', $link); $this->assertStringContainsString('Post Link Title', $link); } /** * Test post link with delete user method * Logged as normal user * * @return void */ public function testPostLinkAuthorizedAllowedFalseLoggedWithoutRole(): void { $url = [ 'prefix' => false, 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'delete', '00000000-0000-0000-0000-000000000010', ]; $this->AuthLink->expects($this->once()) ->method('isAuthorized') ->with( $this->equalTo($url) ) ->will($this->returnValue(false)); $link = $this->AuthLink->postLink('Post Link Title', $url, [ 'allowed' => true, 'class' => 'link-class', 'confirm' => 'confirmation message', ]); $this->assertEmpty($link); } /** * Test post link with delete user method * * @return void */ public function testPostLinkAuthorizedAllowedFalse(): void { $url = [ 'prefix' => false, 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'delete', '00000000-0000-0000-0000-000000000010', ]; $this->AuthLink->expects($this->once()) ->method('isAuthorized') ->with( $this->equalTo($url) ) ->will($this->returnValue(false)); $link = $this->AuthLink->postLink('Post Link Title', $url, [ 'allowed' => true, 'class' => 'link-class', 'confirm' => 'confirmation message', ]); $this->assertEmpty($link); } }