-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX Shortcode provider does not always request a protected asset gran…
…t, add tests for FlysystemAssetStore
- Loading branch information
1 parent
16b3d18
commit 4668fab
Showing
3 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Assets\Tests\Flysystem; | ||
|
||
use League\Flysystem\Filesystem; | ||
use PHPUnit_Framework_MockObject_MockObject; | ||
use SilverStripe\Assets\Flysystem\FlysystemAssetStore; | ||
use SilverStripe\Assets\Flysystem\ProtectedAssetAdapter; | ||
use SilverStripe\Assets\Flysystem\PublicAssetAdapter; | ||
use SilverStripe\Dev\SapphireTest; | ||
|
||
class FlysystemAssetStoreTest extends SapphireTest | ||
{ | ||
/** | ||
* @var PublicAssetAdapter | ||
*/ | ||
protected $publicAdapter; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
protected $publicFilesystem; | ||
|
||
/** | ||
* @var ProtectedAssetAdapter | ||
*/ | ||
protected $protectedAdapter; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
protected $protectedFilesystem; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->publicAdapter = $this->getMockBuilder(PublicAssetAdapter::class) | ||
->setMethods(['getPublicUrl']) | ||
->getMock(); | ||
|
||
$this->publicFilesystem = $this->getMockBuilder(Filesystem::class) | ||
->setMethods(['has']) | ||
->setConstructorArgs([$this->publicAdapter]) | ||
->getMock(); | ||
|
||
$this->protectedAdapter = $this->getMockBuilder(ProtectedAssetAdapter::class) | ||
->setMethods(['getProtectedUrl']) | ||
->getMock(); | ||
|
||
$this->protectedFilesystem = $this->getMockBuilder(Filesystem::class) | ||
->setMethods(['has']) | ||
->setConstructorArgs([$this->protectedAdapter]) | ||
->getMock(); | ||
} | ||
|
||
public function testGetAsUrlDoesntGrantForPublicAssets() | ||
{ | ||
$this->publicFilesystem->expects($this->once())->method('has')->willReturn(true); | ||
$this->publicAdapter->expects($this->once())->method('getPublicUrl')->willReturn('public.jpg'); | ||
$this->protectedFilesystem->expects($this->never())->method('has'); | ||
|
||
/** @var FlysystemAssetStore|PHPUnit_Framework_MockObject_MockObject $assetStore */ | ||
$assetStore = $this->getMockBuilder(FlysystemAssetStore::class) | ||
->setMethods(['getFileID']) | ||
->getMock(); | ||
$assetStore->expects($this->once())->method('getFileID')->willReturn('public.jpg'); | ||
|
||
$assetStore->setPublicFilesystem($this->publicFilesystem); | ||
$assetStore->setProtectedFilesystem($this->protectedFilesystem); | ||
|
||
$this->assertSame('public.jpg', $assetStore->getAsURL('foo', 'bar')); | ||
} | ||
|
||
/** | ||
* @param boolean $grant | ||
* @dataProvider protectedUrlGrantProvider | ||
*/ | ||
public function testGetAsUrlWithGrant($grant) | ||
{ | ||
$this->publicFilesystem->expects($this->once())->method('has')->willReturn(false); | ||
$this->publicAdapter->expects($this->never())->method('getPublicUrl'); | ||
$this->protectedFilesystem->expects($this->once())->method('has')->willReturn(true); | ||
$this->protectedAdapter->expects($this->once())->method('getProtectedUrl')->willReturn('protected.jpg'); | ||
|
||
/** @var FlysystemAssetStore|PHPUnit_Framework_MockObject_MockObject $assetStore */ | ||
$assetStore = $this->getMockBuilder(FlysystemAssetStore::class) | ||
->setMethods(['getFileID', 'grant']) | ||
->getMock(); | ||
$assetStore->expects($this->once())->method('getFileID')->willReturn('protected.jpg'); | ||
$assetStore->expects($grant ? $this->once() : $this->never())->method('grant'); | ||
|
||
$assetStore->setPublicFilesystem($this->publicFilesystem); | ||
$assetStore->setProtectedFilesystem($this->protectedFilesystem); | ||
|
||
$this->assertSame('protected.jpg', $assetStore->getAsURL('foo', 'bar', 'baz', $grant)); | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
public function protectedUrlGrantProvider() | ||
{ | ||
return [ | ||
[true], | ||
[false], | ||
]; | ||
} | ||
} |