Skip to content

Commit

Permalink
Merge pull request #160 from nerds-and-company/asset-folders-fix
Browse files Browse the repository at this point in the history
Asset folders fix
  • Loading branch information
bvangennep authored Sep 24, 2018
2 parents 4ea1d7d + 72c4842 commit a5f18a2
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 4.0.17 - 2018-09-24
### Fixed
- Fixed backwards compatibility of element index mapper
- Fixed issue with volume and volume folder ids going out of sync

### 4.0.16 - 2018-09-11
### Added
- Added map_source event for mapping custom sources. See the Events section in the README for more.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.16",
"version": "4.0.17",
"name": "nerds-and-company/schematic",
"description": "Craft setup and sync tool",
"type": "craft-plugin",
Expand Down
70 changes: 70 additions & 0 deletions src/Behaviors/SourcesBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use TypeError;
use yii\base\Behavior;
use craft\base\Model;
use craft\records\VolumeFolder;
use NerdsAndCompany\Schematic\Schematic;
use NerdsAndCompany\Schematic\Events\SourceMappingEvent;

Expand All @@ -22,6 +23,9 @@
*/
class SourcesBehavior extends Behavior
{
/** Hack to be able to avoid the active record call in VolumeFolder::findOne() */
public $mockFolder = null;

/**
* Recursively find sources in definition attributes.
*
Expand Down Expand Up @@ -123,6 +127,9 @@ public function getSource(string $fieldType, string $source = null, string $inde
$method = 'getGroupBy';
break;
case 'folder':
$service = $this;
$method = 'getFolderBy';
break;
case 'createFoldersInVolume':
case 'deleteFilesAndFoldersInVolume':
case 'saveAssetInVolume':
Expand Down Expand Up @@ -178,4 +185,67 @@ public function getSource(string $fieldType, string $source = null, string $inde

return null;
}

/**
* Get a folder by id
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*
* @param int $folderId
* @return object
*/
private function getFolderById(int $folderId): \stdClass
{
$folder = Craft::$app->assets->getFolderById($folderId);
if ($folder) {
$volume = $folder->getVolume();
return (object) [
'id' => $folderId,
'handle' => $volume->handle
];
}
return null;
}

/**
* Get folder by volume id
*
* @param int $volumeId
* @return VolumeFolder
*/
private function getFolderByVolumeId(int $volumeId): VolumeFolder
{
return $this->mockFolder ? $this->mockFolder : VolumeFolder::findOne(['volumeId' => $volumeId]);
}

/**
* Set a mock folder for the tests
*
* @param VolumeFolder $mockFolder
*/
public function setMockFolder(VolumeFolder $mockFolder)
{
$this->mockFolder = $mockFolder;
}

/**
* Get a folder by volume handle
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*
* @param string $folderHandle
* @return object
*/
private function getFolderByHandle(string $folderHandle): \stdClass
{
$volume = Craft::$app->volumes->getVolumeByHandle($folderHandle);
if ($volume) {
$folder = $this->getFolderByVolumeId($volume->id);
if ($folder) {
return (object) [
'id' => $folder->id,
'handle' => $folderHandle
];
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/Mappers/ElementIndexMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function import(array $settingDefinitions, array $elementTypes): array
{
foreach ($settingDefinitions as $elementType => $settings) {
// Backwards compatibility
if (class_exists('craft\\elements\\'.$elementType, false)) {
if (class_exists('craft\\elements\\'.$elementType)) {
$elementType = 'craft\\elements\\'.$elementType;
}
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
Expand Down
5 changes: 5 additions & 0 deletions tests/_support/Helper/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Helper;

use Craft;
use Yii;
use craft\console\Application;
use yii\console\Controller;
use craft\i18n\I18n;
use craft\services\Assets;
use craft\services\AssetTransforms;
use craft\services\Categories;
use craft\services\Content;
Expand Down Expand Up @@ -50,6 +52,7 @@ public function _before(TestCase $test)
$mockApp->controller->module = $this->getMockModule($test);

Craft::$app = $mockApp;
Yii::$app = $mockApp;
Schematic::$force = false;
}

Expand Down Expand Up @@ -83,6 +86,7 @@ private function getMockModule(TestCase $test)
private function getMockApp(TestCase $test)
{
$mockApp = $this->getMock($test, Application::class);
$mockAssets = $this->getMock($test, Assets::class);
$mockAssetTransforms = $this->getMock($test, AssetTransforms::class);
$mockCategoryGroups = $this->getMock($test, Categories::class);
$mockContent = $this->getMock($test, Content::class);
Expand All @@ -105,6 +109,7 @@ private function getMockApp(TestCase $test)
$mockApp->expects($test->any())
->method('__get')
->willReturnMap([
['assets', $mockAssets],
['assetTransforms', $mockAssetTransforms],
['categories', $mockCategoryGroups],
['content', $mockContent],
Expand Down
64 changes: 53 additions & 11 deletions tests/unit/Converters/Fields/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Craft;
use craft\fields\Assets as AssetsField;
use craft\base\Volume;
use craft\models\VolumeFolder;
use craft\records\VolumeFolder as VolumeFolderRecord;
use Codeception\Test\Unit;

/**
Expand Down Expand Up @@ -43,14 +45,14 @@ protected function _before()
*
* @param AssetsField $assets
* @param array $definition
* @param Mock|Volume|null $mockVolume
* @param Mock|Volume|null $mockFolder
*/
public function testGetRecordDefinition(AssetsField $assets, array $definition, $mockVolume)
public function testGetRecordDefinition(AssetsField $assets, array $definition, $mockFolder)
{
Craft::$app->volumes->expects($this->any())
->method('getVolumeById')
->with($mockVolume->id)
->willReturn($mockVolume);
Craft::$app->assets->expects($this->any())
->method('getFolderById')
->with($mockFolder->id)
->willReturn($mockFolder);

$result = $this->converter->getRecordDefinition($assets);

Expand All @@ -62,18 +64,20 @@ public function testGetRecordDefinition(AssetsField $assets, array $definition,
*
* @param AssetsField $assets
* @param array $definition
* @param Mock|Volume|null $mockVolume
* @param Mock|Volume|null $mockFolder
*/
public function testSetRecordAttributes(AssetsField $assets, array $definition, $mockVolume)
public function testSetRecordAttributes(AssetsField $assets, array $definition, $mockFolder)
{
$this->setMockFolderRecord($mockFolder);
$mockVolume = $mockFolder->getVolume();
Craft::$app->volumes->expects($this->any())
->method('getVolumeByHandle')
->with($mockVolume->handle)
->willReturn($mockVolume);

$newAssets = new AssetsField();

$this->converter->setRecordAttributes($newAssets, $definition, []);

$this->assertSame($assets->name, $newAssets->name);
$this->assertSame($assets->handle, $newAssets->handle);
$this->assertSame($assets->defaultUploadLocationSource, $newAssets->defaultUploadLocationSource);
Expand All @@ -91,13 +95,13 @@ public function testSetRecordAttributes(AssetsField $assets, array $definition,
public function provideAssets()
{
$mockAssets1 = $this->getMockAssets(1, 1);
$mockVolume1 = $this->getMockVolume(1);
$mockFolder1 = $this->getMockFolder(1);

return [
'assets' => [
'Assets' => $mockAssets1,
'definition' => $this->getMockAssetsDefinition($mockAssets1),
'volume' => $mockVolume1,
'volume' => $mockFolder1,
],
];
}
Expand Down Expand Up @@ -194,6 +198,30 @@ private function getMockFieldGroup(int $groupId)
return $mockGroup;
}

/**
* Get a mock folder.
*
* @param int $folderId
*
* @return Mock|VolumeFolder
*/
private function getMockFolder(int $folderId)
{
$mockVolume = $this->getMockVolume($folderId);
$mockFolder = $this->getMockBuilder(VolumeFolder::class)
->disableOriginalConstructor()
->getmock();

$mockFolder->id = $folderId;
$mockFolder->handle = 'folderHandle'.$folderId;

$mockFolder->expects($this->any())
->method('getVolume')
->willReturn($mockVolume);

return $mockFolder;
}

/**
* Get a mock volume.
*
Expand All @@ -212,4 +240,18 @@ private function getMockVolume(int $volumeId)

return $mockVolume;
}

/**
* Set a mock folder record on converter based on mock folder
*
* @param VolumeFolder $mockFolder
*/
private function setMockFolderRecord(VolumeFolder $mockFolder)
{
$mockFolderRecord = $this->getMockBuilder(VolumeFolderRecord::class)->getMock();
$mockFolderRecord->expects($this->any())
->method('__get')
->willReturnMap([['id', $mockFolder->id]]);
$this->converter->setMockFolder($mockFolderRecord);
}
}

0 comments on commit a5f18a2

Please sign in to comment.