Skip to content

Commit

Permalink
Fix Bug #137 (#139) (#140)
Browse files Browse the repository at this point in the history
* Redis returns array instead of deserialized object
  • Loading branch information
R0Wi authored Mar 9, 2024
1 parent 280c5c3 commit 21fa0b8
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lib/Model/CroppingConfigModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@ class CroppingConfigModel {

/** @var float|null */
public $poseRoll;

public static function fromArray(array $data) {
$croppingConfig = new CroppingConfigModel();
$croppingConfig->fullWidth = $data['fullWidth'];
$croppingConfig->fullHeight = $data['fullHeight'];
$croppingConfig->croppedWidth = $data['croppedWidth'];
$croppingConfig->croppedHeight = $data['croppedHeight'];
$croppingConfig->croppedX = $data['croppedX'];
$croppingConfig->croppedY = $data['croppedY'];
$croppingConfig->poseHeading = $data['poseHeading'];
$croppingConfig->posePitch = $data['posePitch'];
$croppingConfig->poseRoll = $data['poseRoll'];
return $croppingConfig;
}
}
8 changes: 8 additions & 0 deletions lib/Model/XmpResultModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function __construct() {
$this->croppingConfig = new CroppingConfigModel();
}

public static function fromArray(array $data) {
$xmpResult = new XmpResultModel();
$xmpResult->usePanoramaViewer = $data['usePanoramaViewer'];
$xmpResult->containsCroppingConfig = $data['containsCroppingConfig'];
$xmpResult->croppingConfig = CroppingConfigModel::fromArray($data['croppingConfig']);
return $xmpResult;
}

public function xmlSerialize(Writer $writer) {
$writer->write([
'usePanoramaViewer' => $this->usePanoramaViewer,
Expand Down
2 changes: 1 addition & 1 deletion lib/Sabre/PhotosphereViewerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private function getXmpMetadataCached(File $file) : ?XmpResultModel {

if ($cachedXmpMeta !== null) {
$this->logger->debug('Cache hit for file {file}', ['file' => $file->getName()]);
return $cachedXmpMeta;
return $cachedXmpMeta instanceof XmpResultModel ? $cachedXmpMeta : XmpResultModel::fromArray($cachedXmpMeta);
}

$this->logger->debug('Cache miss for file {file}', ['file' => $file->getName()]);
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Model/CroppingConfigModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Robin Windey <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


namespace OCA\Files_PhotoSpheres\Tests\Unit\Model;

use OCA\Files_PhotoSpheres\Model\CroppingConfigModel;
use PHPUnit\Framework\TestCase;

class CroppingConfigModelTest extends TestCase {
public function testFromArray() {
$croppingConfig = CroppingConfigModel::fromArray([
'fullWidth' => 1,
'fullHeight' => 2,
'croppedWidth' => 3,
'croppedHeight' => 4,
'croppedX' => 5,
'croppedY' => 6,
'poseHeading' => 7,
'posePitch' => 8,
'poseRoll' => 9,
]);

$this->assertEquals(1, $croppingConfig->fullWidth);
$this->assertEquals(2, $croppingConfig->fullHeight);
$this->assertEquals(3, $croppingConfig->croppedWidth);
$this->assertEquals(4, $croppingConfig->croppedHeight);
$this->assertEquals(5, $croppingConfig->croppedX);
$this->assertEquals(6, $croppingConfig->croppedY);
$this->assertEquals(7, $croppingConfig->poseHeading);
$this->assertEquals(8, $croppingConfig->posePitch);
$this->assertEquals(9, $croppingConfig->poseRoll);
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Model/XmpResultModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,36 @@ public function testCanBeSerializedAndDeserialized() {

$this->assertEquals($xmpModel, $deserialized);
}

public function testFromArray() {
$data = [
'usePanoramaViewer' => false,
'containsCroppingConfig' => true,
'croppingConfig' => [
'fullWidth' => 1,
'fullHeight' => 2,
'croppedWidth' => 3,
'croppedHeight' => 4,
'croppedX' => 5,
'croppedY' => 6,
'poseHeading' => 7,
'posePitch' => 8,
'poseRoll' => 9,
]
];

$xmpModel = XmpResultModel::fromArray($data);

$this->assertEquals($data['usePanoramaViewer'], $xmpModel->usePanoramaViewer);
$this->assertEquals($data['containsCroppingConfig'], $xmpModel->containsCroppingConfig);
$this->assertEquals($data['croppingConfig']['fullWidth'], $xmpModel->croppingConfig->fullWidth);
$this->assertEquals($data['croppingConfig']['fullHeight'], $xmpModel->croppingConfig->fullHeight);
$this->assertEquals($data['croppingConfig']['croppedWidth'], $xmpModel->croppingConfig->croppedWidth);
$this->assertEquals($data['croppingConfig']['croppedHeight'], $xmpModel->croppingConfig->croppedHeight);
$this->assertEquals($data['croppingConfig']['croppedX'], $xmpModel->croppingConfig->croppedX);
$this->assertEquals($data['croppingConfig']['croppedY'], $xmpModel->croppingConfig->croppedY);
$this->assertEquals($data['croppingConfig']['poseHeading'], $xmpModel->croppingConfig->poseHeading);
$this->assertEquals($data['croppingConfig']['posePitch'], $xmpModel->croppingConfig->posePitch);
$this->assertEquals($data['croppingConfig']['poseRoll'], $xmpModel->croppingConfig->poseRoll);
}
}
77 changes: 75 additions & 2 deletions tests/Unit/Sabre/PhotosphereViewerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File;
use OCA\Files_PhotoSpheres\Model\XmpResultModel;
use OCA\Files_PhotoSpheres\Sabre\PhotosphereViewerPlugin;
use OCA\Files_PhotoSpheres\Service\Helper\IXmpDataReader;
use OCP\Files\FileInfo;
Expand Down Expand Up @@ -387,14 +388,86 @@ public function testHandleGetProperties_CreatesDirectoryCache() {
$dir1 = $this->createMock(Directory::class);

$node = $this->createMock(ICollection::class);
$node->expects($this->once())
$node->expects($this->atLeastOnce())
->method('getChildren')
->willReturn([$file1, $file2, $dir1]);

$xmpReader->expects($this->exactly(2))
$xmpReader->expects($this->exactly(2)) // 2 reads, one per file. Should be cached
->method('readXmpDataFromFileObject');

$plugin->handleGetProperties($propFind, $node);
$plugin->handleGetProperties($propFind, $node);
}

public function testReturnsXmpResultModel_IfRedisCacheReturnsArray() { // #137
$this->cacheArray = [
'42' => [
'usePanoramaViewer' => false,
'containsCroppingConfig' => true,
'croppingConfig' => [
'fullWidth' => 1,
'fullHeight' => 2,
'croppedWidth' => 3,
'croppedHeight' => 4,
'croppedX' => 5,
'croppedY' => 6,
'poseHeading' => 7,
'posePitch' => 8,
'poseRoll' => 9,
]
]
];
$logger = $this->createMock(LoggerInterface::class);
$xmpReader = $this->createMock(IXmpDataReader::class);

$plugin = new PhotosphereViewerPlugin(
$xmpReader,
$this->cacheFactory,
$logger
);
$propFind = $this->createMock(PropFind::class);
$propFind->expects($this->once())
->method('getStatus')
->with(
$this->equalTo(self::META_PROP)
)
->willReturn(200);
$propFind->expects($this->never())
->method('getDepth')
->willReturn(1);
$propFindHandleFunction = null;
$propFind->expects($this->once())
->method('handle')
->with(
$this->equalTo(self::META_PROP),
$this->callback(function ($propFindHandleFunctionCall) use (&$propFindHandleFunction) {
$propFindHandleFunction = $propFindHandleFunctionCall;
return true;
})
);

$view = $this->createMock(View::class);
$ocFile = $this->createMock(\OC\Files\Node\File::class);
$ocFile
->method('getId')
->willReturn('42');
$ocFile
->method('getMimetype')
->willReturn('image/jpeg');
$manager = $this->createMock(IManager::class);
$request = $this->createMock(IRequest::class);
$l10n = $this->createMock(IL10N::class);

$node = new File($view, $ocFile, $manager, $request, $l10n);

$plugin->handleGetProperties($propFind, $node);

$this->assertNotNull($propFindHandleFunction);
$this->assertIsCallable($propFindHandleFunction);

$result = $propFindHandleFunction($node);

$this->assertInstanceOf(XmpResultModel::class, $result);
}
}

Expand Down

0 comments on commit 21fa0b8

Please sign in to comment.