Completed
Push — ezp-31420-alignment-for-master ( 207152...004bf5 )
by
unknown
12:09
created

DeleteTranslationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 31.76 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 27
loc 85
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A createTestContentWithLanguages() 27 27 2
A findContent() 0 15 1
A testDeleteContentTranslation() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\SearchService;
10
11
use eZ\Publish\API\Repository\Tests\BaseTest;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
15
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
16
17
/**
18
 * Test case for delete content translation with the SearchService.
19
 *
20
 * @see \eZ\Publish\API\Repository\SearchService
21
 * @group integration
22
 * @group search
23
 */
24
class DeleteTranslationTest extends BaseTest
25
{
26
    protected function setUp(): void
27
    {
28
        $this->markTestSkipped('Not possible to run on ezpublish-kernel:master');
29
    }
30
31
    /**
32
     * @param array $languages
33
     *
34
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
35
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
36
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
37
     */
38 View Code Duplication
    protected function createTestContentWithLanguages(array $languages): Content
39
    {
40
        $repository = $this->getRepository();
41
        $contentTypeService = $repository->getContentTypeService();
42
        $contentService = $repository->getContentService();
43
        $locationService = $repository->getLocationService();
44
45
        $contentTypeArticle = $contentTypeService->loadContentTypeByIdentifier('article');
46
        $contentCreateStructArticle = $contentService->newContentCreateStruct(
47
            $contentTypeArticle,
48
            'eng-GB'
49
        );
50
51
        foreach ($languages as $langCode => $title) {
52
            $contentCreateStructArticle->setField('title', $title, $langCode);
53
        }
54
55
        $locationCreateStructArticle = $locationService->newLocationCreateStruct(2);
56
        $draftArticle = $contentService->createContent(
57
            $contentCreateStructArticle,
58
            [$locationCreateStructArticle]
59
        );
60
        $content = $contentService->publishVersion($draftArticle->getVersionInfo());
61
        $this->refreshSearch($repository);
62
63
        return $content;
64
    }
65
66
    /**
67
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
68
     */
69
    protected function findContent(string $text, string $languageCode): SearchResult
70
    {
71
        $repository = $this->getRepository();
72
        $searchService = $repository->getSearchService();
73
        $query = new Query();
74
        $query->query = new Criterion\FullText($text);
75
        $query->limit = 0;
76
        $languageFilter = [
77
            'languages' => [$languageCode],
78
            'useAlwaysAvailable' => true,
79
            'excludeTranslationsFromAlwaysAvailable' => false,
80
        ];
81
82
        return $searchService->findContent($query, $languageFilter);
83
    }
84
85
    /**
86
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
87
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
88
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
89
     */
90
    public function testDeleteContentTranslation(): void
91
    {
92
        $repository = $this->getRepository();
93
        $testContent = $this->createTestContentWithLanguages(
94
            [
95
                'eng-GB' => 'Contact',
96
                'ger-DE' => 'Kontakt',
97
            ]
98
        );
99
        $contentService = $repository->getContentService();
100
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
101
        $this->assertEquals(1, $searchResult->totalCount);
102
103
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
104
        $this->refreshSearch($repository);
105
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
106
        $this->assertEquals(0, $searchResult->totalCount);
107
    }
108
}
109