Completed
Push — EZP-31420 ( 3f85b8...2cc1cf )
by
unknown
25:11
created

testDeleteContentTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacyElasticsearch;
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 SearchServiceDeleteTranslationTest extends BaseTest
25
{
26
    /**
27
     * @throws \ErrorException
28
     */
29
    public function setUp()
30
    {
31
        $setupFactory = $this->getSetupFactory();
32
33
        if ($setupFactory instanceof LegacyElasticsearch) {
34
            $this->markTestIncomplete('Not implemented for Elasticsearch Search Engine');
35
        }
36
37
        parent::setUp();
38
    }
39
40
    /**
41
     * @param array $languages
42
     * @return \eZ\Publish\API\Repository\Values\Content\Content
43
     *
44
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
45
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
46
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
47
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
48
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
49
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
50
     */
51
    protected function createTestContentWithLanguages(array $languages): Content
52
    {
53
        $repository = $this->getRepository();
54
        $contentTypeService = $repository->getContentTypeService();
55
        $contentService = $repository->getContentService();
56
        $locationService = $repository->getLocationService();
57
58
        $contentTypeArticle = $contentTypeService->loadContentTypeByIdentifier('article');
59
        $contentCreateStructArticle = $contentService->newContentCreateStruct(
60
            $contentTypeArticle,
61
            'eng-GB'
62
        );
63
64
        foreach ($languages as $langCode => $title) {
65
            $contentCreateStructArticle->setField('title', $title, $langCode);
66
            $contentCreateStructArticle->setField(
67
                'intro',
68
                '<?xml version="1.0" encoding="UTF-8"?>
69
<section xmlns="http://docbook.org/ns/docbook" version="5.0-variant ezpublish-1.0">
70
  <para>' . $title . '</para>
71
</section>',
72
                $langCode
73
            );
74
        }
75
76
        $locationCreateStructArticle = $locationService->newLocationCreateStruct(2);
77
        $draftArticle = $contentService->createContent(
78
            $contentCreateStructArticle,
79
            [$locationCreateStructArticle]
80
        );
81
        $content = $contentService->publishVersion($draftArticle->getVersionInfo());
82
        $this->refreshSearch($repository);
83
84
        return $content;
85
    }
86
87
    /**
88
     * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
89
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
90
     */
91
    protected function findContent(string $text, string $languageCode): SearchResult
92
    {
93
        $repository = $this->getRepository();
94
        $searchService = $repository->getSearchService();
95
        $query = new Query();
96
        $query->query = new Criterion\FullText($text);
97
        $query->limit = 0;
98
        $languageFilter = [
99
            'languages' => [$languageCode],
100
            'useAlwaysAvailable' => true,
101
            'excludeTranslationsFromAlwaysAvailable' => false,
102
        ];
103
104
        return $searchService->findContent($query, $languageFilter);
105
    }
106
107
    /**
108
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
109
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
110
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
111
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
112
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
113
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
114
     */
115
    public function testDeleteContentTranslation()
116
    {
117
        $repository = $this->getRepository();
118
        $testContent = $this->createTestContentWithLanguages(
119
            [
120
                'eng-GB' => 'Contact',
121
                'ger-DE' => 'Kontakt',
122
            ]
123
        );
124
        $contentService = $repository->getContentService();
125
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
126
        $this->assertEquals(1, $searchResult->totalCount);
127
128
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
129
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
130
        $this->assertEquals(0, $searchResult->totalCount);
131
    }
132
}
133