ContentRepositoryTest   F
last analyzed

Coupling/Cohesion

Components 1
Dependencies 11

Complexity

Total Complexity 51

Size/Duplication

Total Lines 745
Duplicated Lines 10.87 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 51
c 1
b 0
f 0
lcom 1
cbo 11
dl 81
loc 745
rs 1.6842

38 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testTestUniquenessInContext() 0 7 1
A provideTestUniquenessInContext() 0 7 1
A testFindOneByContentId() 0 6 1
A provideFindOneByContentId() 0 7 1
A testFindPublishedVersion() 0 6 1
A testFindOnePublished() 0 6 1
A provideFindPublishedVersion() 7 7 1
A testFindNotDeletedSortByUpdatedAt() 0 5 1
A testCountNotDeletedByLanguage() 0 5 1
A provideFindNotDeletedSortByUpdatedAt() 7 7 1
A testFindByContentTypeAndCondition() 0 9 1
B provideContentTypeKeywordAndCount() 0 37 1
A testFindByLanguage() 0 9 2
A provideFindByContentIdAndLanguage() 0 7 1
A testFindOneByLanguageAndVersion() 0 7 1
A provideFindOneByContentIdAndLanguageAndVersion() 7 7 1
B testFindForPaginateFilterByContentTypeSiteAndLanguage() 0 34 2
A provideContentTypeAndPaginateAndSearchAndsiteId() 0 15 1
A testCountFilterByContentTypeSiteAndLanguage() 0 5 1
A testFindByHistoryAndSiteId() 7 7 1
A provideFindByHistoryAndSiteId() 0 10 1
B testUpdateStatusByContentType() 0 30 1
A testRemoveVersion() 13 13 1
A generateColumnsProvider() 0 17 4
B getDescriptionColumnEntity() 0 79 1
A assertSameContent() 0 15 4
A testHasStatusedElement() 7 7 1
A findAllPublishedByContentId() 0 6 1
A testFindElementToAutoPublish() 0 7 1
A testFindElementToAutoUnpublish() 0 7 1
B testFindLastVersion() 0 27 1
A testSoftDeleteAndRestoreContent() 0 20 3
A testHasContentIdWithoutAutoUnpublishToState() 0 4 1
A provideContentIdforHasContentNotOffline() 0 7 1
A replaceKeywordLabelById() 18 18 4
A testUpdateEmbeddedStatus() 15 15 1
A provideCountByContentTypeAndSiteInLastVersion() 0 11 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ContentRepositoryTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ContentRepositoryTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace OpenOrchestra\FunctionalTests\ModelBundle\Repository;
4
5
use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractKernelTestCase;
6
use OpenOrchestra\Pagination\Configuration\PaginateFinderConfiguration;
7
use OpenOrchestra\ModelInterface\Repository\ContentRepositoryInterface;
8
use OpenOrchestra\ModelInterface\Repository\RepositoryTrait\KeywordableTraitInterface;
9
use OpenOrchestra\ModelInterface\ContentEvents;
10
use Phake;
11
use OpenOrchestra\ModelBundle\Document\ContentType;
12
use OpenOrchestra\ModelBundle\Document\Content;
13
14
/**
15
 * Class ContentRepositoryTest
16
 *
17
 * @group integrationTest
18
 */
19
class ContentRepositoryTest extends AbstractKernelTestCase
20
{
21
    /**
22
     * @var ContentRepositoryInterface
23
     */
24
    protected $repository;
25
    protected $keywordRepository;
26
    protected $userRepository;
27
    protected $statusRepository;
28
    protected $contentTypeRepository;
29
30
    protected $currentsiteManager;
31
32
    /**
33
     * Set up test
34
     */
35
    protected function setUp()
36
    {
37
        parent::setUp();
38
        static::bootKernel();
39
        $this->keywordRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.keyword');
40
        $this->userRepository = static::$kernel->getContainer()->get('open_orchestra_user.repository.user');
41
        $this->currentsiteManager = Phake::mock('OpenOrchestra\BaseBundle\Context\CurrentSiteIdInterface');
42
        Phake::when($this->currentsiteManager)->getCurrentSiteId()->thenReturn('2');
43
        Phake::when($this->currentsiteManager)->getCurrentSiteDefaultLanguage()->thenReturn('fr');
44
45
        $this->repository = static::$kernel->getContainer()->get('open_orchestra_model.repository.content');
46
        $this->statusRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.status');
47
        $this->contentTypeRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.content_type');
48
    }
49
50
    /**
51
     * @param string  $name
52
     * @param boolean $exists
53
     *
54
     * @dataProvider provideTestUniquenessInContext
55
     */
56
    public function testTestUniquenessInContext($name, $exists)
57
    {
58
        $test = $this->repository->testUniquenessInContext($name);
59
60
        $this->assertEquals($exists, $test);
61
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function provideTestUniquenessInContext()
68
    {
69
        return array(
70
            array('welcome', true),
71
            array('fakeContentId', false),
72
        );
73
    }
74
75
    /**
76
     * @param string $contentId
77
     *
78
     * @dataProvider provideFindOneByContentId
79
     */
80
    public function testFindOneByContentId($contentId)
81
    {
82
        $content = $this->repository->findOneByContentId($contentId);
83
        $this->assertSameContent(null, null, null, $contentId, $content);
84
        $this->assertEquals($contentId, $content->getContentId());
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function provideFindOneByContentId()
91
    {
92
        return array(
93
            array('notre_vision'),
94
            array('bien_vivre_en_france'),
95
        );
96
    }
97
98
    /**
99
     * @param $contentId
100
     * @param $version
101
     * @param string|null $language
102
     *
103
     * @dataProvider provideFindPublishedVersion
104
     */
105
    public function testFindPublishedVersion($contentId, $version, $language)
106
    {
107
        $content = $this->repository->findPublishedVersion($contentId, $language);
108
        $this->assertSameContent($language, $version, null, $contentId, $content);
109
        $this->assertEquals($contentId, $content->getContentId());
110
    }
111
112
    /**
113
     * @param $contentId
114
     * @param $version
115
     * @param string|null $language
116
     *
117
     * @dataProvider provideFindPublishedVersion
118
     */
119
    public function testFindOnePublished($contentId, $version, $language)
120
    {
121
        $content = $this->repository->findOnePublished($contentId, $language, '2');
122
        $this->assertSameContent($language, $version, null, $contentId, $content);
123
        $this->assertEquals($contentId, $content->getContentId());
124
    }
125
126
    /**
127
     * @return array
128
     */
129 View Code Duplication
    public function provideFindPublishedVersion()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        return array(
132
            array('notre_vision', '1', 'fr'),
133
            array('bien_vivre_en_france', '1', 'fr'),
134
        );
135
    }
136
137
    /**
138
     * @param string $contentId
139
     * @param string $language
140
     * @param int    $count
141
     *
142
     * @dataProvider provideFindNotDeletedSortByUpdatedAt
143
     */
144
    public function testFindNotDeletedSortByUpdatedAt($contentId, $language, $count)
145
    {
146
        $contents = $this->repository->findNotDeletedSortByUpdatedAt($contentId, $language);
147
        $this->assertCount($count, $contents);
148
    }
149
150
    /**
151
     * @param string $contentId
152
     * @param string $language
153
     * @param int    $count
154
     *
155
     * @dataProvider provideFindNotDeletedSortByUpdatedAt
156
     */
157
    public function testCountNotDeletedByLanguage($contentId, $language, $count)
158
    {
159
        $countContents = $this->repository->countNotDeletedByLanguage($contentId, $language);
160
        $this->assertSame($count, $countContents);
161
    }
162
163
    /**
164
     * @return array
165
     */
166 View Code Duplication
    public function provideFindNotDeletedSortByUpdatedAt()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        return array(
169
            array('notre_vision', 'fr', 1),
170
            array('bien_vivre_en_france', 'fr', 1),
171
        );
172
    }
173
174
    /**
175
     * @param string      $contentType
176
     * @param string      $choiceType
177
     * @param string|null $keywords
178
     * @param int         $count
179
     *
180
     * @dataProvider provideContentTypeKeywordAndCount
181
     */
182
    public function testFindByContentTypeAndCondition($contentType = '', $choiceType, $keywords = null, $count)
183
    {
184
        $keywords = $this->replaceKeywordLabelById($keywords);
185
186
        $language = $this->currentsiteManager->getCurrentSiteDefaultLanguage();
0 ignored issues
show
Bug introduced by Noël GILAIN
The method getCurrentSiteDefaultLanguage() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
        $elements = $this->repository->findByContentTypeAndCondition($language, $contentType, $choiceType, $keywords);
188
189
        $this->assertCount($count, $elements);
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function provideContentTypeKeywordAndCount()
196
    {
197
        return array(
198
            array('car', ContentRepositoryInterface::CHOICE_AND, 'lorem', 3),
199
            array('car',ContentRepositoryInterface::CHOICE_AND, 'sit', 1),
200
            array('car', ContentRepositoryInterface::CHOICE_AND, 'dolor', 0),
201
            array('car', ContentRepositoryInterface::CHOICE_AND, 'sit AND lorem', 1),
202
            array('news', ContentRepositoryInterface::CHOICE_AND, 'lorem', 1),
203
            array('news', ContentRepositoryInterface::CHOICE_AND, 'sit', 2),
204
            array('news', ContentRepositoryInterface::CHOICE_AND, 'dolor', 0),
205
            array('news', ContentRepositoryInterface::CHOICE_AND, 'lorem AND sit', 1),
206
            array('news', ContentRepositoryInterface::CHOICE_AND, '', 4),
207
            array('car', ContentRepositoryInterface::CHOICE_AND, '', 3),
208
            array('', ContentRepositoryInterface::CHOICE_AND, '', 9),
209
            array('', ContentRepositoryInterface::CHOICE_AND, '', 9),
210
            array('', ContentRepositoryInterface::CHOICE_AND, 'lorem', 5),
211
            array('', ContentRepositoryInterface::CHOICE_AND, 'sit', 4),
212
            array('', ContentRepositoryInterface::CHOICE_AND, 'dolor', 0),
213
            array('', ContentRepositoryInterface::CHOICE_AND, 'lorem AND sit', 3),
214
            array('car', ContentRepositoryInterface::CHOICE_OR, 'lorem', 5),
215
            array('car', ContentRepositoryInterface::CHOICE_OR, 'sit', 6),
216
            array('car', ContentRepositoryInterface::CHOICE_OR, 'dolor', 3),
217
            array('car', ContentRepositoryInterface::CHOICE_OR, 'lorem AND sit', 5),
218
            array('news', ContentRepositoryInterface::CHOICE_OR, 'lorem', 8),
219
            array('news', ContentRepositoryInterface::CHOICE_OR, 'sit', 6),
220
            array('news', ContentRepositoryInterface::CHOICE_OR, 'dolor', 4),
221
            array('news', ContentRepositoryInterface::CHOICE_OR, 'lorem AND sit', 6),
222
            array('news', ContentRepositoryInterface::CHOICE_OR, '', 4),
223
            array('car', ContentRepositoryInterface::CHOICE_OR, '', 3),
224
            array('', ContentRepositoryInterface::CHOICE_OR, '', 9),
225
            array('', ContentRepositoryInterface::CHOICE_OR, 'lorem', 5),
226
            array('', ContentRepositoryInterface::CHOICE_OR, 'sit', 4),
227
            array('', ContentRepositoryInterface::CHOICE_OR, 'dolor', 0),
228
            array('', ContentRepositoryInterface::CHOICE_OR, 'lorem AND sit', 3),
229
            array('', ContentRepositoryInterface::CHOICE_OR, '', 9),
230
        );
231
    }
232
233
    /**
234
     * @param string $contentId
235
     * @param string $language
236
     *
237
     * @dataProvider provideFindByContentIdAndLanguage
238
     */
239
    public function testFindByLanguage($contentId, $language)
240
    {
241
        $contents = $this->repository->findByLanguage($contentId, $language);
0 ignored issues
show
Bug introduced by Noël GILAIN
The method findByLanguage() does not seem to exist on object<OpenOrchestra\Mod...entRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
242
243
        foreach ($contents as $content) {
244
            $this->assertSameContent($language, null, null, $contentId, $content);
245
        }
246
247
    }
248
249
    /**
250
     * @return array
251
     */
252
    public function provideFindByContentIdAndLanguage()
253
    {
254
        return array(
255
            array('notre_vision', 'fr'),
256
            array('bien_vivre_en_france', 'fr'),
257
        );
258
    }
259
260
    /**
261
     * @param string $contentId
262
     * @param string $language
263
     * @param int    $version
264
     *
265
     * @dataProvider provideFindOneByContentIdAndLanguageAndVersion
266
     */
267
    public function testFindOneByLanguageAndVersion($contentId, $language, $version)
268
    {
269
        $content = $this->repository->findOneByLanguageAndVersion($contentId, $language, $version);
270
271
        $this->assertSameContent($language, $version, null, $contentId, $content);
272
273
    }
274
275
    /**
276
     * @return array
277
     */
278 View Code Duplication
    public function provideFindOneByContentIdAndLanguageAndVersion()
0 ignored issues
show
Duplication introduced by Noël GILAIN
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
    {
280
        return array(
281
            array('notre_vision', 'fr', '1'),
282
            array('bien_vivre_en_france', 'fr', '1'),
283
        );
284
    }
285
286
    /**
287
     * @param string     $contentType
288
     * @param array|null $search
289
     * @param array|null $order
290
     * @param string     $siteId
291
     * @param int        $skip
292
     * @param int        $limit
293
     * @param string     $language
294
     * @param integer    $count
295
     * @param integer    $totalCount
296
     * @param string     $name
297
     *
298
     * @dataProvider provideContentTypeAndPaginateAndSearchAndsiteId
299
     */
300
    public function testFindForPaginateFilterByContentTypeSiteAndLanguage($contentType, $search, $order, $siteId, $skip, $limit, $language, $count, $totalCount, $name = null)
301
    {
302
        $mapping = array(
303
            'name' => 'name',
304
            'status_label' => 'status.labels.'.$language,
305
            'linked_to_site' => 'linkedToSite',
306
            'created_at' => 'createdAt',
307
            'created_by' => 'createdBy',
308
            'updated_at' => 'updatedAt',
309
            'updated_by' => 'updatedBy',
310
            'fields.car_name.string_value' => 'attributes.car_name.stringValue',
311
            'fields.description.string_value' => 'attributes.description.stringValue',
312
            'fields.on_market.string_value' => 'attributes.on_market.stringValue',
313
            'fields.tinimce_test.string_value' => 'attributes.tinimce_test.stringValue',
314
        );
315
316
        $searchTypes = array (
317
            'attributes.car_name' => 'text',
318
            'attributes.description' => 'text',
319
            'attributes.on_market' => 'date',
320
            'attributes.tinimce_test' => 'text',
321
        );
322
323
324
        $configuration = PaginateFinderConfiguration::generateFromVariable($order, $skip, $limit, $mapping, $search);
325
        $contents = $this->repository->findForPaginateFilterByContentTypeSiteAndLanguage($configuration, $contentType, $siteId, $language, $searchTypes);
0 ignored issues
show
Unused Code introduced by itkg-nanne
The call to ContentRepositoryInterfa...ntTypeSiteAndLanguage() has too many arguments starting with $searchTypes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
326
        $repositoryCount = $this->repository->countWithFilterAndContentTypeSiteAndLanguage($configuration, $contentType, $siteId, $language, $searchTypes);
0 ignored issues
show
Unused Code introduced by itkg-nanne
The call to ContentRepositoryInterfa...ntTypeSiteAndLanguage() has too many arguments starting with $searchTypes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
327
328
        if(!is_null($name)) {
329
            $this->assertEquals($name, $contents[0]->getName());
330
        }
331
        $this->assertCount($count, $contents);
332
        $this->assertEquals($totalCount, $repositoryCount);
333
    }
334
335
    /**
336
     * @return array
337
     */
338
    public function provideContentTypeAndPaginateAndSearchAndsiteId()
339
    {
340
        return array(
341
            1  => array('car', null, array("name" => "name", "dir" => "asc"), '2', 0 ,5 , 'en', 3, 3, '206 3 portes en'),
342
            2  => array('car', null, array("name" => "name", "dir" => "desc"), '2', 0 ,5 , 'en', 3, 3, 'R5 3 portes en'),
343
            3  => array('car', null, array("name" => "fields.car_name.string_value", "dir" => "asc"), '2', 0 ,5 , 'en', 3, 3, '206 3 portes en'),
344
            4  => array('car', null, array("name" => "fields.car_name.string_value", "dir" => "desc"),'2', 0 ,5 , 'en', 3, 3, 'R5 3 portes en'),
345
            5  => array('car', null, null, null, 0 ,1 , 'en', 1, 2),
346
            6  => array('car', array('attributes.car_name' => '206'), null, '2', 0 , 2 , 'en', 1, 1),
347
            7  => array('news', null, null, '2', 0 , 100, 'fr', 4, 4),
348
            8  => array('news', null, null, '2', 50 , 100, 'en', 0, 0),
349
            9 => array('news', array('name' => 'news'), null, '2', 0 , null, 'fr', 0, 0),
350
            10 => array('car', null, null, '2', 0 ,5 , 'en', 3, 3),
351
        );
352
    }
353
354
    /**
355
     * @param string  $contentType
356
     * @param string  $siteId
357
     * @param string  $language
358
     * @param integer $count
359
     *
360
     * @dataProvider provideCountByContentTypeAndSiteInLastVersion
361
     */
362
    public function testCountFilterByContentTypeSiteAndLanguage($contentType, $siteId, $language, $count)
363
    {
364
        $contents = $this->repository->countFilterByContentTypeSiteAndLanguage($contentType, $siteId, $language);
365
        $this->assertEquals($count, $contents);
366
    }
367
368
    /**
369
     * @return array
370
     */
371
    public function provideCountByContentTypeAndSiteInLastVersion()
372
    {
373
        return array(
374
            array('car', '1', 'en', 2),
375
            array('car', '2', 'en', 3),
376
            array('customer', '1', 'en', 1),
377
            array('customer', '2', 'en', 1),
378
            array('news', '1', 'en', 0),
379
            array('news', '2', 'fr', 4),
380
        );
381
    }
382
383
    /**
384
     * @param string       $user
385
     * @param string       $siteId
386
     * @param array        $eventTypes
387
     * @param boolean|null $published
388
     * @param int          $limit
389
     * @param array|null   $sort
390
     * @param int          $count
391
     *
392
     * @dataProvider provideFindByHistoryAndSiteId
393
     */
394 View Code Duplication
    public function testFindByHistoryAndSiteId($user, $siteId, array $eventTypes, $published, $limit, $sort, $count)
0 ignored issues
show
Duplication introduced by itkg-nanne
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
395
    {
396
        $user = $this->userRepository->findOneByUsername($user);
397
398
        $contents = $this->repository->findByHistoryAndSiteId($user->getId(), $siteId, $eventTypes, $published, $limit, $sort);
399
        $this->assertCount($count, $contents);
400
    }
401
402
    /**
403
     * @return array
404
     */
405
    public function provideFindByHistoryAndSiteId()
406
    {
407
        return array(
408
            1 => array('p-admin', '2', array(ContentEvents::CONTENT_CREATION), null, 10, array('updatedAt' => -1), 0),
409
            2 => array('p-admin', '2', array(ContentEvents::CONTENT_CREATION), false, 10, null, 0),
410
            3 => array('p-admin', '2', array(ContentEvents::CONTENT_CREATION), true, 10, null, 0),
411
            4 => array('p-admin', '2', array(ContentEvents::CONTENT_UPDATE), true, 10, null, 0),
412
            5 => array('p-admin', '2', array(ContentEvents::CONTENT_CREATION, ContentEvents::CONTENT_UPDATE), true, 10, null, 0),
413
        );
414
    }
415
416
    /**
417
     * test updateStatusByContentType
418
     */
419
    public function testUpdateStatusByContentType()
420
    {
421
        $contentTypeId = 'test';
422
423
        $outOfWorkflow = $this->statusRepository->findOneByOutOfWorkflow();
424
425
        $dm = $this->contentTypeRepository->getDocumentManager();
426
427
        $contentType = new ContentType();
428
        $contentType->setContentTypeId($contentTypeId);
429
        $dm->persist($contentType);
430
        $dm->flush();
431
432
        $content = new Content();
433
        $content->setContentType($contentTypeId);
434
        $dm->persist($content);
435
        $dm->flush();
436
437
        $this->repository->updateStatusByContentType($outOfWorkflow, $contentTypeId);
438
        $dm->clear();
439
440
        $updatedContent = $this->repository->findOneBy(array('_id' => $content->getId()));
0 ignored issues
show
Bug introduced by itkg-nanne
The method findOneBy() does not exist on OpenOrchestra\ModelInter...tentRepositoryInterface. Did you maybe mean findOneByContentId()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
441
        $this->assertEquals('outOfWorkflow', $updatedContent->getStatus()->getName());
442
443
        $contentType = $this->contentTypeRepository->findOneBy(array('contentTypeId' => $contentTypeId));
444
445
        $dm->remove($updatedContent);
446
        $dm->remove($contentType);
447
        $dm->flush();
448
    }
449
450
    /**
451
     * Test remove content version
452
     */
453 View Code Duplication
    public function testRemoveVersion()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
454
    {
455
        $dm = static::$kernel->getContainer()->get('object_manager');
456
        $content = $this->repository->findOneByLanguageAndVersion('bien_vivre_en_france', 'fr', "1");
457
        $storageIds = array($content->geTId());
458
        $dm->detach($content);
459
460
        $this->repository->removeContentVersion($storageIds);
461
        $this->assertNull($this->repository->findOneByLanguageAndVersion('bien_vivre_en_france', 'fr', "1"));
462
463
        $dm->persist($content);
464
        $dm->flush();
465
    }
466
467
    /**
468
     * Generate columns of content with search value
469
     *
470
     * @param array|null $searchColumns
471
     * @param string     $globalSearch
472
     *
473
     * @return array
474
     */
475
    protected function generateColumnsProvider($searchColumns = null, $globalSearch = '')
476
    {
477
        $search = array();
478
        if (null !== $searchColumns) {
479
            $columns = array();
480
            foreach ($searchColumns as $name => $value) {
481
                $columns[$name] = $value;
482
            }
483
            $search['columns'] = $columns;
484
        }
485
486
        if (!empty($globalSearch)) {
487
            $search['global'] = $globalSearch;
488
        }
489
490
        return $search;
491
    }
492
493
    /**
494
     * Generate relation between columns names and entities attributes
495
     *
496
     * @return array
497
     */
498
    protected function getDescriptionColumnEntity()
499
    {
500
        return array (
501
            'name' =>
502
            array (
503
                'key' => 'name',
504
                'field' => 'name',
505
                'type' => 'string',
506
            ),
507
            'language' =>
508
            array (
509
                'key' => 'language',
510
                'field' => 'language',
511
                'type' => 'string',
512
            ),
513
            'status_label' =>
514
            array (
515
                'key' => 'status_label',
516
                'field' => 'status',
517
                'type' => 'multiLanguages',
518
            ),
519
            'version' =>
520
            array (
521
                'key' => 'version',
522
                'field' => 'version',
523
                'type' => 'integer',
524
            ),
525
            'linked_to_site' =>
526
            array (
527
                'key' => 'linked_to_site',
528
                'field' => 'linkedTosite',
529
                'type' => 'boolean',
530
            ),
531
            'created_by' =>
532
            array (
533
                'key' => 'created_by',
534
                'field' => 'createdBy',
535
                'type' => 'string',
536
            ),
537
            'updated_by' =>
538
            array (
539
                'key' => 'updated_by',
540
                'field' => 'updatedBy',
541
                'type' => 'string',
542
            ),
543
            'created_at' =>
544
            array (
545
                'key' => 'created_at',
546
                'field' => 'createdAt',
547
                'type' => 'date',
548
            ),
549
            'updated_at' =>
550
            array (
551
                'key' => 'updated_at',
552
                'field' => 'updatedAt',
553
                'type' => 'date',
554
            ),
555
            'deleted' =>
556
            array (
557
                'key' => 'deleted',
558
                'field' => 'deleted',
559
                'type' => 'boolean',
560
            ),
561
            'attributes.car_name.string_value' =>
562
            array(
563
                'key' => 'attributes.car_name.string_value',
564
                'field' => 'attributes.car_name.stringValue',
565
                'type' => 'string',
566
                'value' => NULL,
567
            ),
568
            'attributes.description.string_value' =>
569
            array(
570
                'key' => 'attributes.description.string_value',
571
                'field' => 'attributes.description.stringValue',
572
                'type' => 'string',
573
                'value' => NULL,
574
            ),
575
        );
576
    }
577
578
    /**
579
     * @param string                                               $language
580
     * @param int                                                  $version
581
     * @param string                                               $siteId
582
     * @param \OpenOrchestra\ModelInterface\Model\ContentInterface $content
583
     * @param string                                               $contentId
584
     */
585
    protected function assertSameContent($language, $version, $siteId, $contentId, $content)
586
    {
587
        $this->assertInstanceOf('OpenOrchestra\ModelInterface\Model\ContentInterface', $content);
588
        $this->assertSame($contentId, $content->getContentId());
589
        if (!is_null($language)) {
590
            $this->assertSame($language, $content->getLanguage());
591
        }
592
        if (!is_null($version)) {
593
            $this->assertSame($version, $content->getVersion());
594
        }
595
        if (!is_null($siteId)) {
596
            $this->assertSame($siteId, $content->getsiteId());
597
        }
598
        $this->assertSame(false, $content->isDeleted());
599
    }
600
601
    /**
602
     * Test has statused element
603
     */
604 View Code Duplication
    public function testHasStatusedElement()
0 ignored issues
show
Duplication introduced by Noël GILAIN
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
605
    {
606
        $statusRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.status');
607
        $status = $statusRepository->findOneByInitial();
608
609
        $this->assertTrue($this->repository->hasStatusedElement($status));
610
    }
611
612
    /**
613
     * Test findAllPublishedByContentId
614
     */
615
    public function findAllPublishedByContentId()
616
    {
617
        $contents = $this->repository->findAllPublishedByContentId('bien_vivre_en_france');
618
619
        $this->assertEquals(1, count($contents));
620
    }
621
622
    /**
623
     * Test findElementToAutoPublish
624
     */
625
    public function testFindElementToAutoPublish()
626
    {
627
        $fromStatus = $this->statusRepository->findByAutoPublishFrom();
628
        $contents = $this->repository->findElementToAutoPublish('2', $fromStatus);
629
630
        $this->assertEquals(0, count($contents));
631
    }
632
633
    /**
634
     * Test findElementToAutoUnpublish
635
     */
636
    public function testFindElementToAutoUnpublish()
637
    {
638
        $publishedStatus = $this->statusRepository->findOneByPublished();
639
        $contents = $this->repository->findElementToAutoUnpublish('2', $publishedStatus);
640
641
        $this->assertEquals(0, count($contents));
642
    }
643
644
    /**
645
     * Test find last version
646
     */
647
    public function testFindLastVersion()
648
    {
649
        $documentManager = static::$kernel->getContainer()->get('object_manager');
650
        $contentId = 'test-find-last-version';
651
652
        $firstContent = new Content();
653
        $firstContent->setCreatedAt(new \DateTime('2017-02-27T15:03:01.012345Z'));
654
        $firstContent->setContentId($contentId);
655
        $firstContent->setContentType('car');
656
        $documentManager->persist($firstContent);
657
658
        $lastContent = new Content();
659
        $lastContent->setCreatedAt(new \DateTime('2017-02-28T15:03:01.012345Z'));
660
        $lastContent->setContentId($contentId);
661
        $lastContent->setContentType('car');
662
        $documentManager->persist($lastContent);
663
664
        $documentManager->flush();
665
666
        $content = $this->repository->findLastVersion($contentId);
667
        $this->assertEquals((new \DateTime('2017-02-28T15:03:01.012345Z'))->getTimestamp(), $content->getCreatedAt()->getTimestamp());
668
669
        $documentManager->remove($firstContent);
670
        $documentManager->remove($lastContent);
671
672
        $documentManager->flush();
673
    }
674
675
    /**
676
     * Test soft delete content
677
     */
678
    public function testSoftDeleteAndRestoreContent()
679
    {
680
        $contentId = 'bien_vivre_en_france';
681
682
        $this->repository->softDeleteContent($contentId);
683
        $contents = $this->repository->findByContentId($contentId);
684
        foreach ($contents as $content ) {
685
            $this->assertTrue($content->isDeleted());
686
        }
687
        $this->repository->restoreDeletedContent($contentId);
688
689
        $documentManager = static::$kernel->getContainer()->get('object_manager');
690
        $documentManager->clear();
691
        $documentManager->close();
692
693
        $contents = $this->repository->findByContentId($contentId);
694
        foreach ($contents as $content ) {
695
            $this->assertFalse($content->isDeleted());
696
        }
697
    }
698
699
    /**
700
     * @param string $contentId
701
     * @param bool   $has
702
     *
703
     * @dataProvider provideContentIdforHasContentNotOffline
704
     */
705
    public function testHasContentIdWithoutAutoUnpublishToState($contentId, $has)
706
    {
707
        $this->assertSame($has, $this->repository->hasContentIdWithoutAutoUnpublishToState($contentId));
708
    }
709
710
    /**
711
     * @return array
712
     */
713
    public function provideContentIdforHasContentNotOffline()
714
    {
715
        return array(
716
            array('bien_vivre_en_france', true),
717
            array('notre_vision', true)
718
        );
719
    }
720
721
    /**
722
     * @param string $condition
723
     *
724
     * @return array
725
     */
726 View Code Duplication
    protected function replaceKeywordLabelById($condition)
0 ignored issues
show
Duplication introduced by Noël GILAIN
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
727
    {
728
        $conditionWithoutOperator = preg_replace(explode('|', KeywordableTraitInterface::OPERATOR_SPLIT), ' ', $condition);
729
        $conditionArray = explode(' ', $conditionWithoutOperator);
730
731
        foreach ($conditionArray as $keyword) {
732
            if ($keyword != '') {
733
                $keywordDocument = $this->keywordRepository->findOneByLabel($keyword);
734
                if (!is_null($keywordDocument)) {
735
                    $condition = str_replace($keyword, $keywordDocument->getId(), $condition);
736
                } else {
737
                    return '';
738
                }
739
            }
740
        }
741
742
        return $condition;
743
    }
744
745
    /**
746
     * Test update embedded status
747
     */
748 View Code Duplication
    public function testUpdateEmbeddedStatus()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
749
    {
750
        $statusRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.status');
751
        $status = $statusRepository->findOneByName('published');
752
        $fakeColor = 'fakeColor';
753
        $saveColor = $status->getDisplayColor();
754
        $status->setDisplayColor($fakeColor);
755
        $this->repository->updateEmbeddedStatus($status);
756
757
        $content = $this->repository->findOnePublished('bien_vivre_en_france', 'fr', '2');
758
        $this->assertEquals($fakeColor, $content->getStatus()->getDisplayColor());
759
760
        $status->setDisplayColor($saveColor);
761
        $this->repository->updateEmbeddedStatus($status);
762
    }
763
}
764