Skip to content

Commit 24e0af3

Browse files
committed
[BUGFIX] Add first product image file reference for new watchlist item
1 parent 098ad91 commit 24e0af3

5 files changed

Lines changed: 231 additions & 10 deletions

File tree

Classes/Domain/DoctrineRepository/Product/ProductRepository.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ public function findProductByUid(int $uid): array|bool
3636
->fetchAssociative();
3737
}
3838

39+
public function findFirstProductImageUid(int $uid): int|bool
40+
{
41+
$queryBuilder = $this
42+
->connectionPool
43+
->getConnectionForTable('sys_file_reference')
44+
->createQueryBuilder()
45+
;
46+
47+
$queryBuilder
48+
->select('uid')
49+
->from('sys_file_reference')
50+
->where(
51+
$queryBuilder->expr()->and(
52+
$queryBuilder->expr()->eq('tablenames', $queryBuilder->createNamedParameter(self::TABLENAME)),
53+
$queryBuilder->expr()->eq('fieldname', $queryBuilder->createNamedParameter('images')),
54+
$queryBuilder->expr()->eq('uid_foreign', $queryBuilder->createNamedParameter($uid)),
55+
)
56+
)
57+
->orderBy('sorting_foreign')
58+
->setMaxResults(1);
59+
60+
return $queryBuilder
61+
->executeQuery()
62+
->fetchOne()
63+
;
64+
}
65+
3966
public function getStock(int $uid): int
4067
{
4168
$queryBuilder = $this->getQueryBuilder();

Classes/Domain/Model/WatchlistItemFactory.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,10 @@ public function createFromIdentifier(
4040

4141
private function getFirstImageReference(array $product): ?int
4242
{
43-
if (is_string($product['images'] ?? null) === false || $product['images'] === '') {
43+
if ((int)$product['images'] === 0) {
4444
return null;
4545
}
4646

47-
$images = explode(',', $product['images']);
48-
49-
$image = array_pop($images);
50-
51-
if (is_numeric($image) === false) {
52-
return null;
53-
}
54-
55-
return (int)$image;
47+
return $this->productRepository->findFirstProductImageUid($product['uid']);
5648
}
5749
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'sys_file_reference' => [
7+
0 => [
8+
'uid' => 1,
9+
'pid' => 7,
10+
'uid_local' => 42,
11+
'uid_foreign' => 1,
12+
'tablenames' => 'tx_cartproducts_domain_model_product_product',
13+
'fieldname' => 'images',
14+
],
15+
],
16+
];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'sys_file_reference' => [
7+
0 => [
8+
'uid' => 5,
9+
'pid' => 7,
10+
'uid_local' => 42,
11+
'uid_foreign' => 1,
12+
'tablenames' => 'tx_cartproducts_domain_model_product_product',
13+
'fieldname' => 'images',
14+
],
15+
1 => [
16+
'uid' => 16,
17+
'pid' => 7,
18+
'uid_local' => 37,
19+
'uid_foreign' => 1,
20+
'tablenames' => 'tx_cartproducts_domain_model_product_product',
21+
'fieldname' => 'images',
22+
],
23+
],
24+
];
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace Extcode\CartProducts\Tests\Functional\Domain\DoctrineRepository\Product;
4+
5+
use Codappix\Typo3PhpDatasets\TestingFramework;
6+
use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository;
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
10+
use TYPO3\CMS\Core\Database\ConnectionPool;
11+
use TYPO3\CMS\Core\Http\ServerRequest;
12+
use TYPO3\CMS\Core\Utility\GeneralUtility;
13+
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
14+
15+
#[CoversClass(ProductRepository::class)]
16+
class ProductRepositoryTest extends FunctionalTestCase
17+
{
18+
use TestingFramework;
19+
20+
protected ProductRepository $productRepository;
21+
22+
public function setUp(): void
23+
{
24+
$this->testExtensionsToLoad[] = 'extcode/cart';
25+
$this->testExtensionsToLoad[] = 'extcode/cart-products';
26+
27+
$this->coreExtensionsToLoad[] = 'typo3/cms-reactions';
28+
29+
parent::setUp();
30+
31+
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())
32+
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE);
33+
34+
$this->productRepository = GeneralUtility::makeInstance(
35+
ProductRepository::class,
36+
GeneralUtility::makeInstance(
37+
ConnectionPool::class
38+
)
39+
);
40+
41+
$this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Pages.php');
42+
$this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Products.php');
43+
}
44+
45+
#[Test]
46+
public function returnsFalseIfProductHasNoImage(): void
47+
{
48+
self::assertFalse(
49+
$this->productRepository->findFirstProductImageUid(1)
50+
);
51+
}
52+
53+
#[Test]
54+
public function returnsUidOfImageIfProductHasOnlyOneImage(): void
55+
{
56+
$this->importPhpDataSet(__DIR__ . '/Fixtures/OneImageFileReference.php');
57+
58+
self::assertSame(
59+
1,
60+
$this->productRepository->findFirstProductImageUid(1)
61+
);
62+
}
63+
64+
#[Test]
65+
public function returnsFirstUidOfImageIfProductHasMoreThanOneImage(): void
66+
{
67+
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');
68+
69+
self::assertSame(
70+
5,
71+
$this->productRepository->findFirstProductImageUid(1)
72+
);
73+
}
74+
75+
#[Test]
76+
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndRespectsSorting(): void
77+
{
78+
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');
79+
80+
$this
81+
->getConnectionPool()
82+
->getConnectionForTable('sys_file_reference')
83+
->update(
84+
'sys_file_reference',
85+
[
86+
'sorting_foreign' => 128,
87+
],
88+
[
89+
'uid' => 5,
90+
]
91+
)
92+
;
93+
$this
94+
->getConnectionPool()
95+
->getConnectionForTable('sys_file_reference')
96+
->update(
97+
'sys_file_reference',
98+
[
99+
'sorting_foreign' => 16,
100+
],
101+
[
102+
'uid' => 16,
103+
]
104+
)
105+
;
106+
107+
self::assertSame(
108+
16,
109+
$this->productRepository->findFirstProductImageUid(1)
110+
);
111+
}
112+
113+
#[Test]
114+
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsDeleted(): void
115+
{
116+
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');
117+
118+
$this
119+
->getConnectionPool()
120+
->getConnectionForTable('sys_file_reference')
121+
->update(
122+
'sys_file_reference',
123+
[
124+
'deleted' => 1,
125+
],
126+
[
127+
'uid' => 5,
128+
]
129+
)
130+
;
131+
132+
self::assertSame(
133+
16,
134+
$this->productRepository->findFirstProductImageUid(1)
135+
);
136+
}
137+
138+
#[Test]
139+
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsHidden(): void
140+
{
141+
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');
142+
143+
$this
144+
->getConnectionPool()
145+
->getConnectionForTable('sys_file_reference')
146+
->update(
147+
'sys_file_reference',
148+
[
149+
'hidden' => 1,
150+
],
151+
[
152+
'uid' => 5,
153+
]
154+
)
155+
;
156+
157+
self::assertSame(
158+
16,
159+
$this->productRepository->findFirstProductImageUid(1)
160+
);
161+
}
162+
}

0 commit comments

Comments
 (0)