Skip to content

Commit

Permalink
Merge branch 'release/0.5' of https://github.com/sulu/SuluArticleBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Sep 11, 2017
2 parents 1506a97 + 6ee3327 commit 4403657
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 18 deletions.
47 changes: 30 additions & 17 deletions Markup/ArticleLinkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery;
use ONGR\ElasticsearchDSL\Search;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Document\Index\DocumentFactory;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Bundle\ContentBundle\Markup\Link\LinkConfiguration;
use Sulu\Bundle\ContentBundle\Markup\Link\LinkItem;
use Sulu\Bundle\ContentBundle\Markup\Link\LinkProviderInterface;
Expand All @@ -26,31 +25,44 @@
*/
class ArticleLinkProvider implements LinkProviderInterface
{
use ArticleViewDocumentIdTrait;

/**
* @var Manager
*/
private $manager;
private $liveManager;

/**
* @var DocumentFactory
* @var Manager
*/
private $documentFactory;
private $defaultManager;

/**
* @var array
*/
private $types;

/**
* @param Manager $manager
* @param DocumentFactory $documentFactory
* @param array $types
* @var string
*/
public function __construct(Manager $manager, DocumentFactory $documentFactory, array $types)
{
$this->manager = $manager;
$this->documentFactory = $documentFactory;
private $articleViewClass;

/**
* @param Manager $liveManager
* @param Manager $defaultManager
* @param array $types
* @param string $articleViewClass
*/
public function __construct(
Manager $liveManager,
Manager $defaultManager,
array $types,
$articleViewClass
) {
$this->liveManager = $liveManager;
$this->defaultManager = $defaultManager;
$this->types = $types;
$this->articleViewClass = $articleViewClass;
}

/**
Expand Down Expand Up @@ -82,12 +94,13 @@ function ($type) {
public function preload(array $hrefs, $locale, $published = true)
{
$search = new Search();
$search->addQuery(new IdsQuery($hrefs));
if ($published) {
$search->addQuery(new RangeQuery('authored', ['lte' => 'now']));
$search->addQuery(new IdsQuery($this->getViewDocumentIds($hrefs, $locale)));

$repository = $this->liveManager->getRepository($this->articleViewClass);
if (!$published) {
$repository = $this->defaultManager->getRepository($this->articleViewClass);
}

$repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
$documents = $repository->findDocuments($search);

$result = [];
Expand All @@ -97,7 +110,7 @@ public function preload(array $hrefs, $locale, $published = true)
$document->getUuid(),
$document->getTitle(),
$document->getRoutePath(),
$document->getAuthored() <= new \DateTime()
$document->getPublishedState()
);
}

Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@
<!-- link -->
<service id="sulu_article.markup.link_provider" class="Sulu\Bundle\ArticleBundle\Markup\ArticleLinkProvider">
<argument type="service" id="es.manager.live"/>
<argument type="service" id="sulu_article.view_document.factory"/>
<argument type="service" id="es.manager.default"/>
<argument>%sulu_article.types%</argument>
<argument>%sulu_article.view_document.article.class%</argument>

<tag name="sulu.link.provider" alias="article"/>
</service>
Expand Down
109 changes: 109 additions & 0 deletions Tests/Functional/Markup/ArticleLinkProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Functional\Markup;

use ONGR\ElasticsearchBundle\Service\Manager;
use Sulu\Bundle\ArticleBundle\Markup\ArticleLinkProvider;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;

class ArticleLinkProviderTest extends SuluTestCase
{
/**
* @var ArticleLinkProvider
*/
private $articleLinkProvider;

/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();

$this->initPhpcr();

/** @var Manager $manager */
$manager = $this->getContainer()->get('es.manager.default');
$manager->dropAndCreateIndex();
$manager = $this->getContainer()->get('es.manager.live');
$manager->dropAndCreateIndex();

$this->articleLinkProvider = $this->getContainer()->get('sulu_article.markup.link_provider');
}

public function testPreload()
{
$articles = [
$this->createArticle(),
$this->createAndPublishArticle(),
];

$uuids = array_map(
function (array $data) {
return $data['id'];
},
$articles
);

$result = $this->articleLinkProvider->preload($uuids, 'de', false);

$this->assertCount(2, $result);
$this->assertEquals($articles[0]['id'], $result[0]->getId());
$this->assertFalse($result[0]->isPublished());
$this->assertEquals($articles[1]['id'], $result[1]->getId());
$this->assertTrue($result[1]->isPublished());
}

public function testPreloadPublished()
{
$articles = [
$this->createArticle(),
$this->createAndPublishArticle(),
];

$uuids = array_map(
function (array $data) {
return $data['id'];
},
$articles
);

$result = $this->articleLinkProvider->preload($uuids, 'de');

$this->assertCount(1, $result);
$this->assertEquals($articles[1]['id'], $result[0]->getId());
}

private function createArticle($title = 'Test-Article', $template = 'default', $data = [])
{
$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles?locale=de',
array_merge($data, ['title' => $title, 'template' => $template])
);

return json_decode($client->getResponse()->getContent(), true);
}

private function createAndPublishArticle($title = 'Test-Article', $template = 'default', $data = [])
{
$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles?locale=de&action=publish',
array_merge($data, ['title' => $title, 'template' => $template])
);

return json_decode($client->getResponse()->getContent(), true);
}
}

0 comments on commit 4403657

Please sign in to comment.