Port PHRAS-1593

This commit is contained in:
Mike Ng
2017-11-24 16:22:31 +04:00
parent 79493d9dca
commit a79b72fbaf
5 changed files with 230 additions and 149 deletions

View File

@@ -2,6 +2,7 @@
namespace Alchemy\Tests\Phrasea\Controller\Prod;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Controller\Prod\ShareController;
use Alchemy\Phrasea\ControllerProvider\Prod\Share;
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -19,86 +20,63 @@ class ShareTest extends \PhraseanetAuthenticatedWebTestCase
/**
* @covers Alchemy\Phrasea\Controller\Prod\Share::shareRecord
* @covers Alchemy\Phrasea\Controller\Prod\Share::connect
* @covers Alchemy\Phrasea\Controller\Prod\Share::call
*/
public function testMountedRouteSlash()
public function testRouteSlashALL()
{
$url = sprintf('/prod/share/record/%d/%d/', self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id());
self::$DI['client']->request('GET', $url);
$this->assertTrue(self::$DI['client']->getResponse()->isOk());
$this->_RouteSlash("all", [0=>true, 1=>true, 2=>true, 3=>true]);
}
/**
* @covers Alchemy\Phrasea\Controller\Prod\Share::shareRecord
* @covers Alchemy\Phrasea\Controller\Prod\Share::connect
*/
public function testRouteSlash()
public function testRouteSlashPublishers()
{
$stubbedACL = $this->stubACL();
//has_right_on_base return true
$stubbedACL->expects($this->once())
->method('has_right_on_sbas')
->will($this->returnValue(true));
//has_access_to_subdef return true
$stubbedACL->expects($this->once())
->method('has_access_to_subdef')
->will($this->returnValue(true));
$url = sprintf('/prod/share/record/%d/%d/', self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id());
self::$DI['client']->request('GET', $url);
$this->assertTrue(self::$DI['client']->getResponse()->isOk());
$this->_RouteSlash("publishers", [0=>false, 1=>true, 2=>false, 3=>true]);
}
public function testRouteSlashNone()
{
$this->_RouteSlash("none", [0=>false, 1=>false, 2=>false, 3=>false]);
}
private function _RouteSlash($setting, $expected)
{
$app = $this->getApplication();
$_conf = $app['conf'];
$app['conf'] = $this->getMockBuilder('Alchemy\Phrasea\Core\Configuration\PropertyAccess')
->disableOriginalConstructor()
->getMock();
$app['conf']
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($param, $default) use ($_conf, $setting) {
switch ($param) {
case ['registry', 'actions', 'social-tools']:
return $setting;
}
return $_conf->get($param, $default);
}));
$result = [];
foreach($expected as $flags=>$v) {
$stubbedACL = $this->stubACL();
// "has_right_on_sbas" IS checked by the route->before(), the url will return 403
$stubbedACL->expects($this->any())
->method('has_right_on_sbas')
->will($this->returnValue(($flags & 1) ? true:false));
// but "has_access_to_subdef" IS NOT checked (the url will return a 200 with a message "no subdef to share")
$stubbedACL->expects($this->any())
->method('has_access_to_subdef')
->will($this->returnValue(($flags & 2) ? true:false));
$url = sprintf('/prod/share/record/%d/%d/', self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id());
self::$DI['client']->request('GET', $url);
$result[$flags] = self::$DI['client']->getResponse()->isOk();
}
$this->assertEquals($expected, $result);
}
/**
* @covers Alchemy\Phrasea\Controller\Prod\Share::shareRecord
*/
public function testShareRecord()
{
$share = new ShareController(self::$DI['app']);
/** @var \record_adapter $record_1 */
$record_1 = self::$DI['record_1'];
$response = $share->shareRecord($record_1->getBaseId(), $record_1->getRecordId());
$this->assertTrue($response->isOk());
}
/**
* @covers Alchemy\Phrasea\Controller\Prod\Share::shareRecord
*/
public function testShareRecordBadAccess()
{
$share = new ShareController(self::$DI['app']);
$stubbedACL = $this->getMockBuilder('\ACL')
->disableOriginalConstructor()
->getMock();
//has_access_to_subdef return false
$stubbedACL->expects($this->once())
->method('has_access_to_subdef')
->will($this->returnValue(false));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($stubbedACL));
self::$DI['app']['acl'] = $aclProvider;
try {
$share->shareRecord(self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id());
} catch (HttpException $exception) {
$this->assertEquals(403, $exception->getStatusCode());
return;
}
$this->fail('An access denied exception should have been thrown.');
}
}