Completed
Push — ezp-31420-alignment-for-master ( 207152 )
by
unknown
42:59 queued 30:38
created

DeleteTranslationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 33.75 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
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
    /**
27
     * @param array $languages
28
     *
29
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
30
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
31
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
32
     */
33 View Code Duplication
    protected function createTestContentWithLanguages(array $languages): Content
34
    {
35
        $repository = $this->getRepository();
36
        $contentTypeService = $repository->getContentTypeService();
37
        $contentService = $repository->getContentService();
38
        $locationService = $repository->getLocationService();
39
40
        $contentTypeArticle = $contentTypeService->loadContentTypeByIdentifier('article');
41
        $contentCreateStructArticle = $contentService->newContentCreateStruct(
42
            $contentTypeArticle,
43
            'eng-GB'
44
        );
45
46
        foreach ($languages as $langCode => $title) {
47
            $contentCreateStructArticle->setField('title', $title, $langCode);
48
        }
49
50
        $locationCreateStructArticle = $locationService->newLocationCreateStruct(2);
51
        $draftArticle = $contentService->createContent(
52
            $contentCreateStructArticle,
53
            [$locationCreateStructArticle]
54
        );
55
        $content = $contentService->publishVersion($draftArticle->getVersionInfo());
56
        $this->refreshSearch($repository);
57
58
        return $content;
59
    }
60
61
    /**
62
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
63
     */
64
    protected function findContent(string $text, string $languageCode): SearchResult
65
    {
66
        $repository = $this->getRepository();
67
        $searchService = $repository->getSearchService();
68
        $query = new Query();
69
        $query->query = new Criterion\FullText($text);
70
        $query->limit = 0;
71
        $languageFilter = [
72
            'languages' => [$languageCode],
73
            'useAlwaysAvailable' => true,
74
            'excludeTranslationsFromAlwaysAvailable' => false,
75
        ];
76
77
        return $searchService->findContent($query, $languageFilter);
78
    }
79
80
    /**
81
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
82
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
83
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
84
     */
85
    public function testDeleteContentTranslation(): void
86
    {
87
        $repository = $this->getRepository();
88
        $testContent = $this->createTestContentWithLanguages(
89
            [
90
                'eng-GB' => 'Contact',
91
                'ger-DE' => 'Kontakt',
92
            ]
93
        );
94
        $contentService = $repository->getContentService();
95
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
96
        $this->assertEquals(1, $searchResult->totalCount);
97
98
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
99
        $this->refreshSearch($repository);
100
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
101
        $this->assertEquals(0, $searchResult->totalCount);
102
    }
103
}
104