diff --git a/Controller/ArticleController.php b/Controller/ArticleController.php index c0be41f2e..b2c60ae20 100644 --- a/Controller/ArticleController.php +++ b/Controller/ArticleController.php @@ -15,8 +15,10 @@ use FOS\RestBundle\Routing\ClassResourceInterface; use JMS\Serializer\SerializationContext; use ONGR\ElasticsearchBundle\Service\Manager; +use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\IdsQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; +use ONGR\ElasticsearchDSL\Query\MatchQuery; use ONGR\ElasticsearchDSL\Query\MultiMatchQuery; use ONGR\ElasticsearchDSL\Query\TermQuery; use ONGR\ElasticsearchDSL\Sort\FieldSort; @@ -123,6 +125,14 @@ public function cgetAction(Request $request) $search->addQuery(new TermQuery('type', $type)); } + if (null !== ($contactId = $request->get('contactId'))) { + $boolQuery = new BoolQuery(); + $boolQuery->add(new MatchQuery('changer_contact_id', $contactId), BoolQuery::SHOULD); + $boolQuery->add(new MatchQuery('creator_contact_id', $contactId), BoolQuery::SHOULD); + $boolQuery->add(new MatchQuery('author_id', $contactId), BoolQuery::SHOULD); + $search->addQuery($boolQuery); + } + if (null === $search->getQueries()) { $search->addQuery(new MatchAllQuery()); } diff --git a/Document/ArticleViewDocument.php b/Document/ArticleViewDocument.php index ef43fbca0..4cfed18d0 100644 --- a/Document/ArticleViewDocument.php +++ b/Document/ArticleViewDocument.php @@ -225,6 +225,27 @@ class ArticleViewDocument implements ArticleViewDocumentInterface */ protected $localizationState; + /** + * @var string + * + * @Property(type="string") + */ + protected $authorId; + + /** + * @var string + * + * @Property(type="string") + */ + protected $creatorContactId; + + /** + * @var string + * + * @Property(type="string") + */ + protected $changerContactId; + /** * @param string $uuid */ @@ -607,4 +628,58 @@ public function setLocalizationState(LocalizationStateViewObject $localizationSt return $this; } + + /** + * {@inheritdoc} + */ + public function setAuthorId($authorId) + { + $this->authorId = $authorId; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getAuthorId() + { + return $this->authorId; + } + + /** + * {@inheritdoc} + */ + public function setCreatorContactId($creatorContactId) + { + $this->creatorContactId = $creatorContactId; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCreatorContactId() + { + return $this->creatorContactId; + } + + /** + * {@inheritdoc} + */ + public function setChangerContactId($changerContactId) + { + $this->changerContactId = $changerContactId; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getChangerContactId() + { + return $this->changerContactId; + } } diff --git a/Document/ArticleViewDocumentInterface.php b/Document/ArticleViewDocumentInterface.php index db11d6475..4ad2d237d 100644 --- a/Document/ArticleViewDocumentInterface.php +++ b/Document/ArticleViewDocumentInterface.php @@ -307,7 +307,7 @@ public function setTeaserMediaId($teaserMediaId); /** * Get published. * - * @return $this + * @return \DateTime */ public function getPublished(); @@ -323,7 +323,7 @@ public function setPublished(\DateTime $published = null); /** * Get published state. * - * @return $this + * @return bool */ public function getPublishedState(); @@ -351,4 +351,52 @@ public function getLocalizationState(); * @return $this */ public function setLocalizationState(LocalizationStateViewObject $localizationState); + + /** + * Set author id. + * + * @param string $authorId + * + * @return $this + */ + public function setAuthorId($authorId); + + /** + * Get author id. + * + * @return string + */ + public function getAuthorId(); + + /** + * Set creator contact id. + * + * @param string $creatorContactId + * + * @return $this + */ + public function setCreatorContactId($creatorContactId); + + /** + * Get creator contact id. + * + * @return string + */ + public function getCreatorContactId(); + + /** + * Set creator contact id. + * + * @param string $changerContactId + * + * @return $this + */ + public function setChangerContactId($changerContactId); + + /** + * Get changer contact id. + * + * @return string + */ + public function getChangerContactId(); } diff --git a/Document/Index/ArticleIndexer.php b/Document/Index/ArticleIndexer.php index 948b7baf7..fbc28a2da 100644 --- a/Document/Index/ArticleIndexer.php +++ b/Document/Index/ArticleIndexer.php @@ -23,7 +23,9 @@ use Sulu\Bundle\ArticleBundle\Event\IndexEvent; use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait; use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait; +use Sulu\Bundle\ContactBundle\Entity\Contact; use Sulu\Bundle\ContactBundle\Entity\ContactRepository; +use Sulu\Bundle\SecurityBundle\Entity\User; use Sulu\Bundle\SecurityBundle\UserManager\UserManager; use Sulu\Component\Content\Document\LocalizationState; use Sulu\Component\Content\Document\WorkflowStage; @@ -199,13 +201,28 @@ protected function createOrUpdateArticle( $article->setCreated($document->getCreated()); $article->setAuthored($document->getAuthored()); if ($document->getAuthor()) { - $article->setAuthorFullName($this->contactRepository->findById($document->getAuthor())->getFullName()); + /** @var Contact $author */ + $author = $this->contactRepository->findById($document->getAuthor()); + if ($author) { + $article->setAuthorFullName($author->getFullName()); + $article->setAuthorId($author->getId()); + } } if ($document->getChanger()) { - $article->setChangerFullName($this->userManager->getFullNameByUserId($document->getChanger())); + /** @var User $changer */ + $changer = $this->userManager->getUserById($document->getChanger()); + if ($changer) { + $article->setChangerFullName($changer->getFullName()); + $article->setChangerContactId($changer->getContact()->getId()); + } } if ($document->getCreator()) { - $article->setCreatorFullName($this->userManager->getFullNameByUserId($document->getCreator())); + /** @var User $creator */ + $creator = $this->userManager->getUserById($document->getCreator()); + if ($creator) { + $article->setCreatorFullName($creator->getFullName()); + $article->setCreatorContactId($creator->getContact()->getId()); + } } $article->setType($this->getType($structureMetadata)); $article->setStructureType($document->getStructureType()); diff --git a/Resources/config/serializer/Document.ArticleViewDocument.xml b/Resources/config/serializer/Document.ArticleViewDocument.xml index 0ce9b3681..22e4f7cb4 100644 --- a/Resources/config/serializer/Document.ArticleViewDocument.xml +++ b/Resources/config/serializer/Document.ArticleViewDocument.xml @@ -14,5 +14,8 @@ + + + diff --git a/Resources/public/dist/components/articles/list/main.js b/Resources/public/dist/components/articles/list/main.js index 4cfe49dd2..8bf233b6a 100644 --- a/Resources/public/dist/components/articles/list/main.js +++ b/Resources/public/dist/components/articles/list/main.js @@ -1 +1 @@ -define(["underscore","services/husky/storage","sulucontent/components/copy-locale-overlay/main","sulucontent/components/open-ghost-overlay/main","services/suluarticle/article-manager"],function(a,b,c,d,e){"use strict";var f={options:{config:{},storageName:"articles"},templates:{list:['
','
','
','
'].join(""),draftIcon:'',publishedIcon:'',route:["articles","<% if (!!type) { %>:<%=type%><% } %>","/<%=locale%>"].join("")},translations:{headline:"sulu_article.list.title",unpublished:"public.unpublished",publishedWithDraft:"public.published-with-draft",openGhostOverlay:{info:"sulu_article.settings.open-ghost-overlay.info","new":"sulu_article.settings.open-ghost-overlay.new",copy:"sulu_article.settings.open-ghost-overlay.copy",ok:"sulu_article.settings.open-ghost-overlay.ok"}}};return{defaults:f,header:function(){this.storage=b.get("sulu",this.options.storageName);var c,d=this.options.config.types,e=this.options.config.typeNames,f={icon:"plus-circle",title:"public.add-new"},g=!1,h=null,i=this.options.type||this.storage.getWithDefault("type",null);return 1===e.length?f.callback=function(){this.toAdd(e[0])}.bind(this):(f.dropdownItems=a.map(e,function(a){return{title:d[a].title,callback:function(){this.toAdd(a)}.bind(this)}}.bind(this)),c=[],this.options.config.displayTabAll===!0&&c.push({name:"public.all",key:null}),a.each(e,function(a){c.push({id:a,name:d[a].title,key:a}),a===i&&(h=d[a].title)}.bind(this)),g={componentOptions:{callback:this.typeChange.bind(this),preselector:"name",preselect:h},data:c}),{noBack:!0,tabs:g,toolbar:{buttons:{addArticle:{options:f},deleteSelected:{}},languageChanger:{data:this.options.config.languageChanger,preSelected:this.options.locale}}}},layout:{content:{width:"max"}},initialize:function(){if(this.options.type)this.storage.set("type",this.options.type);else if(this.storage.has("type")){var a=this.templates.route({type:this.storage.get("type"),locale:this.options.locale});this.sandbox.emit("sulu.router.navigate",a,!1,!1),this.options.type=this.storage.get("type")}this.render(),this.bindCustomEvents()},render:function(){this.$el.html(this.templates.list()),this.sandbox.sulu.initListToolbarAndList.call(this,"article","/admin/api/articles/fields",{el:this.$find(".list-toolbar-container"),instanceName:"articles",template:this.sandbox.sulu.buttons.get({settings:{options:{dropdownItems:[{type:"columnOptions"}]}}})},{el:this.sandbox.dom.find(".datagrid-container"),url:"/admin/api/articles?sortBy=authored&sortOrder=desc&locale="+this.options.locale+(this.options.type?"&type="+this.options.type:""),storageName:this.options.storageName,searchInstanceName:"articles",searchFields:["title"],resultKey:"articles",idKey:"uuid",instanceName:"articles",actionCallback:function(a,b){"ghost"===b.localizationState.state?e.load(a,this.options.locale).then(function(b){d.openGhost.call(this,b,this.translations.openGhostOverlay).then(function(b,d){b?c.copyLocale.call(this,a,d,[this.options.locale],function(){this.toEdit(a)}.bind(this)):this.toEdit(a)}.bind(this))}.bind(this)).fail(function(a){this.sandbox.emit("sulu.article.error",a.status,data)}.bind(this)):this.toEdit(a)}.bind(this),viewOptions:{table:{actionIconColumn:"title",badges:[{column:"title",callback:function(a,b){return!(!a.localizationState||"ghost"!==a.localizationState.state||a.localizationState.locale===this.options.locale)&&(b.title=a.localizationState.locale,b)}.bind(this)},{column:"title",callback:function(a,b){var c="",d=this.translations.unpublished;return a.published&&!a.publishedState&&(d=this.translations.publishedWithDraft,c+=this.templates.publishedIcon({title:d})),a.publishedState||(c+=this.templates.draftIcon({title:d})),b.title=c,b.cssClass="badge-none",b}.bind(this)}]}}})},toEdit:function(a,b){this.sandbox.emit("sulu.router.navigate","articles/"+(b||this.options.locale)+"/edit:"+a+"/details")},toAdd:function(a,b){this.sandbox.emit("sulu.router.navigate","articles/"+(b||this.options.locale)+"/add"+(this.options.config.typeNames.length>1?":"+a:""))},toList:function(a){1!==this.options.config.typeNames.length&&this.options.type?this.sandbox.emit("sulu.router.navigate","articles:"+this.options.type+"/"+(a||this.options.locale)):this.sandbox.emit("sulu.router.navigate","articles/"+(a||this.options.locale))},deleteItems:function(b){this.sandbox.util.save("/admin/api/articles?ids="+b.join(","),"DELETE").then(function(){a.each(b,function(a){this.sandbox.emit("husky.datagrid.articles.record.remove",a)}.bind(this))}.bind(this))},typeChange:function(a){var b=this.templates.route({type:a.key,locale:this.options.locale});this.options.type=a.key,this.sandbox.emit("husky.datagrid.articles.url.update",{page:1,type:a.key}),this.sandbox.emit("sulu.router.navigate",b,!1,!1),this.storage.set("type",a.key)},getCopyLocaleUrl:function(a,b,c){return e.getCopyLocaleUrl(a,b,c)},bindCustomEvents:function(){this.sandbox.on("husky.datagrid.articles.number.selections",function(a){var b=a>0?"enable":"disable";this.sandbox.emit("sulu.header.toolbar.item."+b,"deleteSelected",!1)}.bind(this)),this.sandbox.on("sulu.toolbar.delete",function(){this.sandbox.emit("husky.datagrid.articles.items.get-selected",this.deleteItems.bind(this))}.bind(this)),this.sandbox.on("sulu.header.language-changed",function(a){a.id!==this.options.locale&&(this.sandbox.sulu.saveUserSetting(this.options.config.settingsKey,a.id),this.toList(a.id))}.bind(this))}}}); \ No newline at end of file +define(["underscore","services/husky/storage","sulucontent/components/copy-locale-overlay/main","sulucontent/components/open-ghost-overlay/main","services/suluarticle/article-manager"],function(a,b,c,d,e){"use strict";var f={options:{config:{},storageName:"articles"},templates:{list:['
','
','
','
'].join(""),draftIcon:'',publishedIcon:'',route:["articles","<% if (!!type) { %>:<%=type%><% } %>","/<%=locale%>"].join("")},translations:{headline:"sulu_article.list.title",unpublished:"public.unpublished",publishedWithDraft:"public.published-with-draft",filterMe:"sulu_article.list.filter.me",filterAll:"sulu_article.list.filter.all",openGhostOverlay:{info:"sulu_article.settings.open-ghost-overlay.info","new":"sulu_article.settings.open-ghost-overlay.new",copy:"sulu_article.settings.open-ghost-overlay.copy",ok:"sulu_article.settings.open-ghost-overlay.ok"}}};return{defaults:f,header:function(){this.storage=b.get("sulu",this.options.storageName);var c,d=this.options.config.types,e=this.options.config.typeNames,f={icon:"plus-circle",title:"public.add-new"},g=!1,h=null,i=this.options.type||this.storage.getWithDefault("type",null);return 1===e.length?f.callback=function(){this.toAdd(e[0])}.bind(this):(f.dropdownItems=a.map(e,function(a){return{title:d[a].title,callback:function(){this.toAdd(a)}.bind(this)}}.bind(this)),c=[],this.options.config.displayTabAll===!0&&c.push({name:"public.all",key:null}),a.each(e,function(a){c.push({id:a,name:d[a].title,key:a}),a===i&&(h=d[a].title)}.bind(this)),g={componentOptions:{callback:this.typeChange.bind(this),preselector:"name",preselect:h},data:c}),{noBack:!0,tabs:g,toolbar:{buttons:{addArticle:{options:f},deleteSelected:{}},languageChanger:{data:this.options.config.languageChanger,preSelected:this.options.locale}}}},layout:{content:{width:"max"}},initialize:function(){if(this.options.type)this.storage.set("type",this.options.type);else if(this.storage.has("type")){var a=this.templates.route({type:this.storage.get("type"),locale:this.options.locale});this.sandbox.emit("sulu.router.navigate",a,!1,!1),this.options.type=this.storage.get("type")}this.render(),this.bindCustomEvents()},render:function(){this.$el.html(this.templates.list()),this.sandbox.sulu.initListToolbarAndList.call(this,"article","/admin/api/articles/fields",{el:this.$find(".list-toolbar-container"),instanceName:"articles",template:this.retrieveListToolbarTemplate()},{el:this.sandbox.dom.find(".datagrid-container"),url:"/admin/api/articles?sortBy=authored&sortOrder=desc&locale="+this.options.locale+(this.options.type?"&type="+this.options.type:""),storageName:this.options.storageName,searchInstanceName:"articles",searchFields:["title"],resultKey:"articles",idKey:"uuid",instanceName:"articles",actionCallback:function(a,b){"ghost"===b.localizationState.state?e.load(a,this.options.locale).then(function(b){d.openGhost.call(this,b,this.translations.openGhostOverlay).then(function(b,d){b?c.copyLocale.call(this,a,d,[this.options.locale],function(){this.toEdit(a)}.bind(this)):this.toEdit(a)}.bind(this))}.bind(this)).fail(function(a){this.sandbox.emit("sulu.article.error",a.status,data)}.bind(this)):this.toEdit(a)}.bind(this),viewOptions:{table:{actionIconColumn:"title",badges:[{column:"title",callback:function(a,b){return!(!a.localizationState||"ghost"!==a.localizationState.state||a.localizationState.locale===this.options.locale)&&(b.title=a.localizationState.locale,b)}.bind(this)},{column:"title",callback:function(a,b){var c="",d=this.translations.unpublished;return a.published&&!a.publishedState&&(d=this.translations.publishedWithDraft,c+=this.templates.publishedIcon({title:d})),a.publishedState||(c+=this.templates.draftIcon({title:d})),b.title=c,b.cssClass="badge-none",b}.bind(this)}]}}})},toEdit:function(a,b){this.sandbox.emit("sulu.router.navigate","articles/"+(b||this.options.locale)+"/edit:"+a+"/details")},toAdd:function(a,b){this.sandbox.emit("sulu.router.navigate","articles/"+(b||this.options.locale)+"/add"+(this.options.config.typeNames.length>1?":"+a:""))},toList:function(a){1!==this.options.config.typeNames.length&&this.options.type?this.sandbox.emit("sulu.router.navigate","articles:"+this.options.type+"/"+(a||this.options.locale)):this.sandbox.emit("sulu.router.navigate","articles/"+(a||this.options.locale))},deleteItems:function(b){this.sandbox.util.save("/admin/api/articles?ids="+b.join(","),"DELETE").then(function(){a.each(b,function(a){this.sandbox.emit("husky.datagrid.articles.record.remove",a)}.bind(this))}.bind(this))},typeChange:function(a){var b=this.templates.route({type:a.key,locale:this.options.locale});this.options.type=a.key,this.sandbox.emit("husky.datagrid.articles.url.update",{page:1,type:a.key}),this.sandbox.emit("sulu.router.navigate",b,!1,!1),this.storage.set("type",a.key)},getCopyLocaleUrl:function(a,b,c){return e.getCopyLocaleUrl(a,b,c)},bindCustomEvents:function(){this.sandbox.on("husky.datagrid.articles.number.selections",function(a){var b=a>0?"enable":"disable";this.sandbox.emit("sulu.header.toolbar.item."+b,"deleteSelected",!1)}.bind(this)),this.sandbox.on("sulu.toolbar.delete",function(){this.sandbox.emit("husky.datagrid.articles.items.get-selected",this.deleteItems.bind(this))}.bind(this)),this.sandbox.on("sulu.header.language-changed",function(a){a.id!==this.options.locale&&(this.sandbox.sulu.saveUserSetting(this.options.config.settingsKey,a.id),this.toList(a.id))}.bind(this))},retrieveListToolbarTemplate:function(){return this.sandbox.sulu.buttons.get({contactIdFilter:{options:{icon:"filter",group:2,title:this.translations.filterAll,showTitle:!0,dropdownOptions:{preSelected:"all",idAttribute:"id",markSelected:!0,changeButton:!0,callback:function(a){this.applyFilterToList.call(this,a)}.bind(this)},dropdownItems:[{id:"me",title:this.translations.filterMe},{id:"all",title:this.translations.filterAll}]}}})},applyFilterToList:function(a){var b=null;if(a.id)switch(a.id){case"me":b=this.sandbox.sulu.user.id;break;default:b=null}this.sandbox.emit("husky.datagrid.articles.url.update",{contactId:b})}}}); \ No newline at end of file diff --git a/Resources/public/js/components/articles/list/main.js b/Resources/public/js/components/articles/list/main.js index f88b04c1f..05d77320b 100644 --- a/Resources/public/js/components/articles/list/main.js +++ b/Resources/public/js/components/articles/list/main.js @@ -43,6 +43,8 @@ define([ headline: 'sulu_article.list.title', unpublished: 'public.unpublished', publishedWithDraft: 'public.published-with-draft', + filterMe: 'sulu_article.list.filter.me', + filterAll: 'sulu_article.list.filter.all', openGhostOverlay: { info: 'sulu_article.settings.open-ghost-overlay.info', new: 'sulu_article.settings.open-ghost-overlay.new', @@ -170,17 +172,7 @@ define([ { el: this.$find('.list-toolbar-container'), instanceName: 'articles', - template: this.sandbox.sulu.buttons.get({ - settings: { - options: { - dropdownItems: [ - { - type: 'columnOptions' - } - ] - } - } - }) + template: this.retrieveListToolbarTemplate() }, { el: this.sandbox.dom.find('.datagrid-container'), @@ -335,6 +327,63 @@ define([ this.sandbox.sulu.saveUserSetting(this.options.config.settingsKey, item.id); this.toList(item.id); }.bind(this)); + }, + + /** + * Generates list toolbar buttons. + */ + retrieveListToolbarTemplate: function() { + return this.sandbox.sulu.buttons.get({ + contactIdFilter: { + options: { + icon: 'filter', + group: 2, + title: this.translations.filterAll, + showTitle: true, + dropdownOptions: { + preSelected: 'all', + idAttribute: 'id', + markSelected: true, + changeButton: true, + callback: function(item) { + this.applyFilterToList.call(this, item); + }.bind(this) + }, + dropdownItems: [ + { + id: 'me', + title: this.translations.filterMe + }, + { + id: 'all', + title: this.translations.filterAll + } + ] + } + } + }); + }, + + /** + * Emits the url update event for the list. + * + * @param item {Object} + */ + applyFilterToList: function(item) { + var contactId = null; + + if (!!item.id) { + switch(item.id) { + case 'me': + contactId = this.sandbox.sulu.user.id; + break; + default: + contactId = null; + break; + } + } + + this.sandbox.emit('husky.datagrid.articles.url.update', {contactId: contactId}); } }; }); diff --git a/Resources/translations/sulu/backend.de.xlf b/Resources/translations/sulu/backend.de.xlf index f3037cc23..1ad153196 100644 --- a/Resources/translations/sulu/backend.de.xlf +++ b/Resources/translations/sulu/backend.de.xlf @@ -118,6 +118,14 @@ sulu_article.smart-content.author-full-name Autor (Alphabet) + + sulu_article.list.filter.me + Nur meine + + + sulu_article.list.filter.all + Alle + diff --git a/Resources/translations/sulu/backend.en.xlf b/Resources/translations/sulu/backend.en.xlf index 397ea0484..54ef002d5 100644 --- a/Resources/translations/sulu/backend.en.xlf +++ b/Resources/translations/sulu/backend.en.xlf @@ -118,6 +118,14 @@ sulu_article.smart-content.author-full-name Author (alphabetic) + + sulu_article.list.filter.me + Only mine + + + sulu_article.list.filter.all + All + diff --git a/Tests/Functional/Controller/ArticleControllerTest.php b/Tests/Functional/Controller/ArticleControllerTest.php index 567217916..47e7ba644 100644 --- a/Tests/Functional/Controller/ArticleControllerTest.php +++ b/Tests/Functional/Controller/ArticleControllerTest.php @@ -11,6 +11,7 @@ namespace Functional\Controller; +use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface; use Sulu\Bundle\MediaBundle\DataFixtures\ORM\LoadCollectionTypes; use Sulu\Bundle\MediaBundle\DataFixtures\ORM\LoadMediaTypes; @@ -18,7 +19,9 @@ use Sulu\Bundle\MediaBundle\Entity\CollectionType; use Sulu\Bundle\MediaBundle\Entity\Media; use Sulu\Bundle\MediaBundle\Entity\MediaType; +use Sulu\Bundle\SecurityBundle\UserManager\UserManager; use Sulu\Bundle\TestBundle\Testing\SuluTestCase; +use Sulu\Component\DocumentManager\DocumentManager; /** * Functional testcases for Article API. @@ -69,7 +72,12 @@ public function testPost($title = 'Test-Article', $template = 'default') $client->request( 'POST', '/api/articles?locale=de', - ['title' => $title, 'template' => $template, 'authored' => '2016-01-01', 'author' => 1] + [ + 'title' => $title, + 'template' => $template, + 'authored' => '2016-01-01', + 'author' => $this->getTestUser()->getContact()->getId(), + ] ); $this->assertHttpStatusCode(200, $client->getResponse()); @@ -79,7 +87,7 @@ public function testPost($title = 'Test-Article', $template = 'default') $this->assertEquals(self::$typeMap[$template], $response['articleType']); $this->assertEquals($template, $response['template']); $this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored'])); - $this->assertEquals(1, $response['author']); + $this->assertEquals($this->getTestUser()->getContact()->getId(), $response['author']); return $response; } @@ -109,7 +117,12 @@ public function testPut($title = 'Sulu is awesome', $locale = 'de', $article = n $client->request( 'PUT', '/api/articles/' . $article['id'] . '?locale=' . $locale, - ['title' => $title, 'template' => 'default', 'authored' => '2016-01-01', 'author' => 1] + [ + 'title' => $title, + 'template' => 'default', + 'authored' => '2016-01-01', + 'author' => $this->getTestUser()->getContact()->getId(), + ] ); $this->assertHttpStatusCode(200, $client->getResponse()); @@ -118,7 +131,7 @@ public function testPut($title = 'Sulu is awesome', $locale = 'de', $article = n $this->assertNotEquals($article['title'], $response['title']); $this->assertEquals($title, $response['title']); $this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored'])); - $this->assertEquals(1, $response['author']); + $this->assertEquals($this->getTestUser()->getContact()->getId(), $response['author']); return $article; } @@ -143,7 +156,7 @@ public function testGetGhost() $this->assertNotEquals($article['title'], $response['title']); $this->assertEquals($title, $response['title']); $this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored'])); - $this->assertEquals(1, $response['author']); + $this->assertEquals($this->getTestUser()->getContact()->getId(), $response['author']); $this->assertEquals(['name' => 'ghost', 'value' => 'de'], $response['type']); } @@ -468,6 +481,180 @@ function ($item) { $this->assertContains([$article2['id'], $article2['title']], $items); } + public function testCGetFilterByContactId() + { + $this->purgeDatabase(); + + /** @var UserManager $userManager */ + $userManager = $this->getContainer()->get('sulu_security.user_manager'); + $contactManager = $this->getContainer()->get('sulu_contact.contact_manager'); + /** @var DocumentManager $documentManager */ + $documentManager = $this->getContainer()->get('sulu_document_manager.document_manager'); + + // create contact1 + $contact1 = $contactManager->save( + [ + 'firstName' => 'Testi 1', + 'lastName' => 'Testo 1', + ], + null, + false, + true + ); + + // create contact2 + $contact2 = $contactManager->save( + [ + 'firstName' => 'Testi 2', + 'lastName' => 'Testo 2', + ], + null, + false, + true + ); + + // create contact3 + $contact3 = $contactManager->save( + [ + 'firstName' => 'Testi 3', + 'lastName' => 'Testo 3', + ], + null, + false, + true + ); + + // create user1 + $user1 = $userManager->save( + [ + 'username' => 'testi.testo1', + 'email' => 'testi.testo1@LOL.xyz', + 'password' => 'ThisIsSave!#123', + 'contact' => [ + 'id' => $contact1->getId(), + ], + ], + 'de', + null, + false, + true + ); + + // create user2 + $user2 = $userManager->save( + [ + 'username' => 'testi.testo2', + 'email' => 'testi.testo2@LOL.xyz', + 'password' => 'ThisIsSave!#123', + 'contact' => [ + 'id' => $contact2->getId(), + ], + ], + 'de', + null, + false, + true + ); + + // create user3 + $user3 = $userManager->save( + [ + 'username' => 'testi.testo3', + 'email' => 'testi.testo3@LOL.xyz', + 'password' => 'ThisIsSave!#123', + 'contact' => [ + 'id' => $contact3->getId(), + ], + ], + 'de', + null, + false, + true + ); + + /** @var ArticleDocument $article */ + $article = $documentManager->create('article'); + $article->setTitle('first title'); + $article->setStructureType('default'); + $article->setAuthor($contact3->getId()); + + $documentManager->persist($article, 'de', ['user' => $user1->getId()]); + $documentManager->publish($article, 'de'); + $documentManager->flush(); + + $documentManager->persist($article, 'de', ['user' => $user2->getId()]); + $documentManager->publish($article, 'de'); + $documentManager->flush(); + + // create client + $client = $this->createAuthenticatedClient(); + + // retrieve all articles for user1 + $client->request( + 'GET', + '/api/articles?locale=de&searchFields=title&type=blog&contactId=' . $user1->getContact()->getId() + ); + + $this->assertHttpStatusCode(200, $client->getResponse()); + $response = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(1, $response['total']); + $this->assertCount(1, $response['_embedded']['articles']); + + // retrieve all articles for user2 + $client->request( + 'GET', + '/api/articles?locale=de&searchFields=title&type=blog&contactId=' . $user2->getContact()->getId() + ); + + $this->assertHttpStatusCode(200, $client->getResponse()); + $response = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(1, $response['total']); + $this->assertCount(1, $response['_embedded']['articles']); + + // add article + /** @var ArticleDocument $article */ + $article2 = $documentManager->create('article'); + $article2->setTitle('first title'); + $article2->setStructureType('default'); + + $documentManager->persist($article2, 'de', ['user' => $user1->getId()]); + $documentManager->publish($article2, 'de'); + $documentManager->flush(); + + // retrieve all articles for user1 + $client->request( + 'GET', + '/api/articles?locale=de&searchFields=title&type=blog&contactId=' . $user1->getContact()->getId() + ); + + $this->assertHttpStatusCode(200, $client->getResponse()); + $response = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(2, $response['total']); + $this->assertCount(2, $response['_embedded']['articles']); + + // retrieve all articles for user2 + $client->request( + 'GET', + '/api/articles?locale=de&searchFields=title&type=blog&contactId=' . $user2->getContact()->getId() + ); + + $this->assertHttpStatusCode(200, $client->getResponse()); + $response = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(1, $response['total']); + $this->assertCount(1, $response['_embedded']['articles']); + + // retrieve all articles for user3 + $client->request( + 'GET', + '/api/articles?locale=de&searchFields=title&type=blog&contactId=' . $user3->getContact()->getId() + ); + + $this->assertHttpStatusCode(200, $client->getResponse()); + $response = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(1, $response['total']); + $this->assertCount(1, $response['_embedded']['articles']); + } + public function testDelete() { $this->purgeIndex();