diff --git a/src/Plugin/FileDownloadUrl.php b/src/Plugin/FileDownloadUrl.php index 9f674f6..dc0669e 100644 --- a/src/Plugin/FileDownloadUrl.php +++ b/src/Plugin/FileDownloadUrl.php @@ -13,10 +13,7 @@ class FileDownloadUrl extends FieldItemList { */ public function __construct(DataDefinitionInterface $definition, $name, TypedDataInterface $parent) { parent::__construct($definition, $name, $parent); - $values = array_map(function ($uri) { - return ['value' => $this::fileCreateRootRelativeUrl($uri['value'])]; - }, $this->getUris()); - $this->setValue($values); + $this->setValue($this->compute()); } /** @@ -32,7 +29,7 @@ class FileDownloadUrl extends FieldItemList { * @return string * The transformed relative URL. */ - public static function fileCreateRootRelativeUrl($uri) { + protected function fileCreateRootRelativeUrl($uri) { return file_url_transform_relative(file_create_url($uri)); } @@ -46,8 +43,21 @@ class FileDownloadUrl extends FieldItemList { * @return array * The array of values. */ - public function getUris() { + protected function getUris() { return $this->getEntity()->get('uri')->getValue(); } + /** + * Computes the file download url field. + * + * @return array + * The array of values to use for the computed field. + */ + public function compute() { + $uri_values = $this->getUris(); + return array_map(function ($uri_value) { + return ['value' => $this->fileCreateRootRelativeUrl($uri_value['value'])]; + }, $uri_values); + } + } diff --git a/tests/src/Unit/Plugin/FileDownloadUrlTest.php b/tests/src/Unit/Plugin/FileDownloadUrlTest.php index 47a2eb2..f88f1d4 100644 --- a/tests/src/Unit/Plugin/FileDownloadUrlTest.php +++ b/tests/src/Unit/Plugin/FileDownloadUrlTest.php @@ -42,12 +42,12 @@ class FileDownloadUrlTest extends UnitTestCase { } /** - * @covers ::__construct + * @covers ::compute */ - public function testConstructor() { + public function testCompute() { $field_list_mock = $this->getMockBuilder(FileDownloadUrl::class) ->setMethods(['getUris', 'fileCreateRootRelativeUrl']) - ->setConstructorArgs([$this->definition, 'lorem', $this->parent]) + ->disableOriginalConstructor() ->getMock(); $field_list_mock @@ -55,14 +55,13 @@ class FileDownloadUrlTest extends UnitTestCase { ->method('getUris') ->will($this->returnValue([['value' => 'public://url.to.file']])); - $uris = $field_list_mock->getUris(); $field_list_mock ->expects($this->once()) ->method('fileCreateRootRelativeUrl') - ->with($this->equalTo([['value' => 'public://url.to.file']])) - ->willReturn([['value' => '/url.to.file']]); + ->with($this->equalTo('public://url.to.file')) + ->willReturn('/url.to.file'); - $this->assertArrayEquals([['value' => '/url.to.file']], $field_list_mock->getValue()); + $this->assertArrayEquals([['value' => '/url.to.file']], $field_list_mock->compute()); } }