From 09dc1281ca800b402de71c0b748b1c9cfccbff11 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 25 May 2018 22:39:33 +0200 Subject: [PATCH] [FEATURE] Add Tea.image as a FAL example (#15) --- Classes/Domain/Model/.gitkeep | 0 Classes/Domain/Model/Product/Tea.php | 30 +++++++++++++++++++ .../TCA/tx_tea_domain_model_product_tea.php | 20 +++++++++++-- .../Private/Language/de.locallang_db.xlf | 4 +++ Resources/Private/Language/locallang_db.xlf | 3 ++ .../Repository/Fixtures/Product/Tea.xml | 17 ++++++++++- .../Repository/Product/TeaRepositoryTest.php | 17 +++++++++++ Tests/Unit/Domain/Model/Product/TeaTest.php | 20 +++++++++++++ ext_tables.sql | 1 + 9 files changed, 109 insertions(+), 3 deletions(-) delete mode 100644 Classes/Domain/Model/.gitkeep diff --git a/Classes/Domain/Model/.gitkeep b/Classes/Domain/Model/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Classes/Domain/Model/Product/Tea.php b/Classes/Domain/Model/Product/Tea.php index 754abf7..322fac2 100644 --- a/Classes/Domain/Model/Product/Tea.php +++ b/Classes/Domain/Model/Product/Tea.php @@ -2,7 +2,9 @@ declare(strict_types = 1); namespace OliverKlee\Tea\Domain\Model\Product; +use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; +use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy; /** * This class represents a tea (flavor), e.g., "Earl Grey". @@ -21,6 +23,12 @@ class Tea extends AbstractEntity */ protected $description = ''; + /** + * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference + * @lazy + */ + protected $image = null; + /** * @return string */ @@ -56,4 +64,26 @@ class Tea extends AbstractEntity { $this->description = $description; } + + /** + * @return FileReference|null + */ + public function getImage() + { + if ($this->image instanceof LazyLoadingProxy) { + $this->image = $this->image->_loadRealInstance(); + } + + return $this->image; + } + + /** + * @param FileReference $image + * + * @return void + */ + public function setImage(FileReference $image) + { + $this->image = $image; + } } diff --git a/Configuration/TCA/tx_tea_domain_model_product_tea.php b/Configuration/TCA/tx_tea_domain_model_product_tea.php index 4abf6e5..7514525 100644 --- a/Configuration/TCA/tx_tea_domain_model_product_tea.php +++ b/Configuration/TCA/tx_tea_domain_model_product_tea.php @@ -13,10 +13,10 @@ return [ 'searchFields' => 'title, description', ], 'interface' => [ - 'showRecordFieldList' => 'title, description', + 'showRecordFieldList' => 'title, description, image', ], 'types' => [ - '1' => ['showitem' => 'title, description'], + '1' => ['showitem' => 'title, description, image'], ], 'columns' => [ 'title' => [ @@ -38,5 +38,21 @@ return [ 'eval' => 'trim', ], ], + 'image' => [ + 'label' => 'LLL:EXT:tea/Resources/Private/Language/locallang_db.xlf:tx_tea_domain_model_product_tea.image', + 'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig( + 'image', + [ + 'maxitems' => 1, + 'appearance' => [ + 'collapseAll' => true, + 'useSortable' => false, + 'enabledControls' => [ + 'hide' => false, + ], + ], + ] + ), + ], ], ]; diff --git a/Resources/Private/Language/de.locallang_db.xlf b/Resources/Private/Language/de.locallang_db.xlf index 77c6869..ca14dd7 100644 --- a/Resources/Private/Language/de.locallang_db.xlf +++ b/Resources/Private/Language/de.locallang_db.xlf @@ -15,6 +15,10 @@ Description Beschreibung + + Image + Bild + diff --git a/Resources/Private/Language/locallang_db.xlf b/Resources/Private/Language/locallang_db.xlf index 545b5d5..5774e22 100644 --- a/Resources/Private/Language/locallang_db.xlf +++ b/Resources/Private/Language/locallang_db.xlf @@ -12,6 +12,9 @@ Description + + Image + diff --git a/Tests/Functional/Domain/Repository/Fixtures/Product/Tea.xml b/Tests/Functional/Domain/Repository/Fixtures/Product/Tea.xml index 874fa2d..25e01ed 100644 --- a/Tests/Functional/Domain/Repository/Fixtures/Product/Tea.xml +++ b/Tests/Functional/Domain/Repository/Fixtures/Product/Tea.xml @@ -10,6 +10,21 @@ 2 1 Assam - Dark ans strong. + Dark and strong. + + 3 + 1 + Gunpowder + Bitter and very green. + 1 + + + 1 + 1 + 3 + tx_tea_domain_model_product_tea + image + + diff --git a/Tests/Functional/Domain/Repository/Product/TeaRepositoryTest.php b/Tests/Functional/Domain/Repository/Product/TeaRepositoryTest.php index 09f1771..a00efd7 100644 --- a/Tests/Functional/Domain/Repository/Product/TeaRepositoryTest.php +++ b/Tests/Functional/Domain/Repository/Product/TeaRepositoryTest.php @@ -6,6 +6,7 @@ use Nimut\TestingFramework\TestCase\FunctionalTestCase; use OliverKlee\Tea\Domain\Model\Product\Tea; use OliverKlee\Tea\Domain\Repository\Product\TeaRepository; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\Object\ObjectManager; /** @@ -84,4 +85,20 @@ class TeaRepositoryTest extends FunctionalTestCase static::assertSame('Earl Grey', $model->getTitle()); static::assertSame('Fresh and hot.', $model->getDescription()); } + + /** + * @test + */ + public function fillsImageRelation() + { + $this->importDataSet(__DIR__ . '/../Fixtures/Product/Tea.xml'); + + $uid = 3; + /** @var Tea $model */ + $model = $this->subject->findByUid($uid); + + $image = $model->getImage(); + static::assertInstanceOf(FileReference::class, $image); + static::assertSame(1, $image->getUid()); + } } diff --git a/Tests/Unit/Domain/Model/Product/TeaTest.php b/Tests/Unit/Domain/Model/Product/TeaTest.php index cf867dc..fe59b12 100644 --- a/Tests/Unit/Domain/Model/Product/TeaTest.php +++ b/Tests/Unit/Domain/Model/Product/TeaTest.php @@ -4,6 +4,7 @@ namespace OliverKlee\Tea\Tests\Unit\Domain\Model\Product; use Nimut\TestingFramework\TestCase\UnitTestCase; use OliverKlee\Tea\Domain\Model\Product\Tea; +use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; /** @@ -68,4 +69,23 @@ class TeaTest extends UnitTestCase static::assertSame($value, $this->subject->getDescription()); } + + /** + * @test + */ + public function getImageInitiallyReturnsNull() + { + static::assertNull($this->subject->getImage()); + } + + /** + * @test + */ + public function setImageSetsImage() + { + $model = new FileReference(); + $this->subject->setImage($model); + + static::assertSame($model, $this->subject->getImage()); + } } diff --git a/ext_tables.sql b/ext_tables.sql index 5b2803c..10e0316 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -10,6 +10,7 @@ CREATE TABLE tx_tea_domain_model_product_tea ( title varchar(255) DEFAULT '' NOT NULL, description varchar(2000) DEFAULT '' NOT NULL, + image int(11) unsigned DEFAULT '0' NOT NULL, PRIMARY KEY (uid), KEY parent (pid)