From 6740a156ad80d6c2e1e7d8d8d076dd4fe6f85d05 Mon Sep 17 00:00:00 2001 From: Pawel Mikolajczuk Date: Thu, 29 Jan 2015 15:58:08 +0100 Subject: [PATCH] add spec and behat test --- features/0_api_articles.feature | 3 +- features/3_api_articles_search.feature | 1 - features/5_api_related_articles.feature | 94 +++++++++++++++++++ .../Newscoop/Services/ArticleService.php | 6 +- .../Services/RelatedArticlesService.php | 4 +- .../library/Newscoop/Services/UserService.php | 2 +- .../Controller/ArticlesController.php | 2 +- .../Services/RelatedArticlesServiceSpec.php | 64 +++++++++++-- 8 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 features/5_api_related_articles.feature diff --git a/features/0_api_articles.feature b/features/0_api_articles.feature index d914d5ea93..ac1cea36d1 100644 --- a/features/0_api_articles.feature +++ b/features/0_api_articles.feature @@ -1,4 +1,4 @@ -Feature: Endpoints +Feature: Articles I need to be able to create and update articles Scenario: Check if route is correct @@ -44,7 +44,6 @@ Feature: Endpoints | fields[content] | <> | 35 | When I submit "article" data to "<>" - Then echo last response Then the response status code should be 200 And the response is JSON And field "keywords" in the response should be "test keywords" diff --git a/features/3_api_articles_search.feature b/features/3_api_articles_search.feature index 79d556c9d5..bd43397292 100644 --- a/features/3_api_articles_search.feature +++ b/features/3_api_articles_search.feature @@ -34,7 +34,6 @@ Feature: Endpoints Given that I want to find an article And I'm logged in as "testuser" with "testpassword" with client "1_svdg45ew371vtsdgd29fgvwe5v" and secret "h48fgsmv0due4nexjsy40jdf3sswwr" When I request "/search/articles?query=unique keyword" - Then echo last response Then the response status code should be 200 And the response is JSON And the response should contain field "items" \ No newline at end of file diff --git a/features/5_api_related_articles.feature b/features/5_api_related_articles.feature new file mode 100644 index 0000000000..bb88e29baa --- /dev/null +++ b/features/5_api_related_articles.feature @@ -0,0 +1,94 @@ +Feature: Testing linking/unlinkig related articles feature + I need to be able to link and unlink articles (aka. related articles) + + Scenario: Create articles and link them them + Given that I want to create an new article + And that i have fake "article" data: + | name | <> | 4 | + | language | 1 || + | publication | 1 || + | issue | || + | section | || + | comments_enabled | 1 || + | type | news || + | onFrontPage | 0 || + | onSection | 0 || + | keywords | <> | 30 | + + And I'm logged in as "testuser" with "testpassword" with client "1_svdg45ew371vtsdgd29fgvwe5v" and secret "h48fgsmv0due4nexjsy40jdf3sswwr" + When I submit "article" data to "/articles/create" + Then the response status code should be 201 + And the response is JSON + And the response should contain field "number" + And the response should contain field "title" + And the response should contain field "type" + Then save new item location as "base_article" + + Given that I want to create an new article + And that i have fake "article" data: + | name | <> | 4 | + | language | 1 || + | publication | 1 || + | issue | || + | section | || + | comments_enabled | 1 || + | type | news || + | onFrontPage | 0 || + | onSection | 0 || + | keywords | <> | 30 | + + And I'm logged in as "testuser" with "testpassword" with client "1_svdg45ew371vtsdgd29fgvwe5v" and secret "h48fgsmv0due4nexjsy40jdf3sswwr" + When I submit "article" data to "/articles/create" + Then the response status code should be 201 + And the response is JSON + And the response should contain field "number" + And the response should contain field "title" + And the response should contain field "type" + Then save new item location as "second_article" + + Given that I want to create an new article + And that i have fake "article" data: + | name | <> | 4 | + | language | 1 || + | publication | 1 || + | issue | || + | section | || + | comments_enabled | 1 || + | type | news || + | onFrontPage | 0 || + | onSection | 0 || + | keywords | <> | 30 | + + And I'm logged in as "testuser" with "testpassword" with client "1_svdg45ew371vtsdgd29fgvwe5v" and secret "h48fgsmv0due4nexjsy40jdf3sswwr" + When I submit "article" data to "/articles/create" + Then the response status code should be 201 + And the response is JSON + And the response should contain field "number" + And the response should contain field "title" + And the response should contain field "type" + Then save new item location as "third_article" + + + Given that I want to link an article to the article + And that i have "link" header with "<$$second_article$$; rel='article'>" value + When I request "<>" + Then the response status code should be 201 + And the response is JSON + + Given that I want to link an article to the article + And that i have "link" header with "<$$third_article$$; rel='article'>" value + When I request "<>" + Then the response status code should be 201 + And the response is JSON + + Given that I want to link an article to the article + And that i have "link" header with "<$$second_article$$; rel='article'>,<2; rel='article-position'>" value + When I request "<>" + Then the response status code should be 201 + And the response is JSON + + Given that I want to unlink an article to the article + And that i have "link" header with "<$$second_article$$; rel='article'>" value + When I request "<>" + Then the response status code should be 204 + And the response is JSON \ No newline at end of file diff --git a/newscoop/library/Newscoop/Services/ArticleService.php b/newscoop/library/Newscoop/Services/ArticleService.php index 4b6f50c0eb..c1f01440ac 100755 --- a/newscoop/library/Newscoop/Services/ArticleService.php +++ b/newscoop/library/Newscoop/Services/ArticleService.php @@ -175,8 +175,8 @@ public function createArticle($articleType, $language, $user, $publication, $att $article->setCommentsLocked(false); //TODO - add this to type $article->setWorkflowStatus('N'); $article->setShortName($article->getNumber()); - $article->setLockTime(new \DateTime('0000:00:00 00:00:00')); - $article->setPublished(new \Datetime('0000:00:00 00:00:00')); + $article->setLockTime(null); + $article->setPublished(new \Datetime()); $article->setUploaded(new \Datetime()); $article->setLockUser(); $article->setPublic(true); @@ -203,7 +203,7 @@ public function updateArticle($article, $attributes) { $this->updateArticleMeta($article, $attributes); $article->setUpdated(new \DateTime()); - $article->setIsIndexed(false); + $article->setIsIndexed('N'); if (array_key_exists('fields', $attributes)) { foreach ($attributes['fields'] as $field => $value) { diff --git a/newscoop/library/Newscoop/Services/RelatedArticlesService.php b/newscoop/library/Newscoop/Services/RelatedArticlesService.php index a70edceaf7..d59ed633c9 100644 --- a/newscoop/library/Newscoop/Services/RelatedArticlesService.php +++ b/newscoop/library/Newscoop/Services/RelatedArticlesService.php @@ -62,13 +62,11 @@ public function removeRelatedArticle($article, $articleToRemove) ->getRelatedArticle($relatedArticles, $articleToRemove->getNumber()) ->getOneOrNullResult(); - $removedRelatedArticlePosition = $relatedArticle->getOrder(); - if ($relatedArticle) { $this->em->remove($relatedArticle); $this->em->flush(); - $this->reorderAfterRemove($relatedArticles, $removedRelatedArticlePosition); + $this->reorderAfterRemove($relatedArticles, $relatedArticle->getOrder()); } return true; diff --git a/newscoop/library/Newscoop/Services/UserService.php b/newscoop/library/Newscoop/Services/UserService.php index f23da74c65..e42e9712e0 100644 --- a/newscoop/library/Newscoop/Services/UserService.php +++ b/newscoop/library/Newscoop/Services/UserService.php @@ -367,7 +367,7 @@ public function savePending($data, User $user) * @param string $lastName * @param integer $publication */ - public function createUser($email, $password, $username, $firstName = null, $lastName = null, $publication = null, $public = true, $userTypes = array()) + public function createUser($email, $password, $username, $firstName = null, $lastName = null, $publication = 0, $public = true, $userTypes = array()) { $users = $this->findBy(array('email' => $email)); if (!empty($users)) { diff --git a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php index 8628553ed1..124dd9e50f 100644 --- a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php +++ b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php @@ -404,7 +404,7 @@ public function getArticleAction(Request $request, $number) * **related articles headers**: * * header name: "link" - * header value: "" + * header value: "" * or with specific language * * header value: "" diff --git a/spec/Newscoop/Services/RelatedArticlesServiceSpec.php b/spec/Newscoop/Services/RelatedArticlesServiceSpec.php index ad20eef554..424b71b270 100644 --- a/spec/Newscoop/Services/RelatedArticlesServiceSpec.php +++ b/spec/Newscoop/Services/RelatedArticlesServiceSpec.php @@ -4,26 +4,78 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Doctrine\ORM\EntityManager; +use Newscoop\Entity\RelatedArticles; +use Newscoop\Entity\RelatedArticle; +use Newscoop\Entity\Article; +use Newscoop\Entity\Repository\RelatedArticleRepository; +use Newscoop\Entity\Repository\RelatedArticlesRepository; +use Doctrine\ORM\AbstractQuery; class RelatedArticlesServiceSpec extends ObjectBehavior { + + public function let( + EntityManager $em, + Article $article, + Article $articleToRemove, + Article $articleToAdd, + RelatedArticles $relatedArticles, + RelatedArticle $relatedArticle, + RelatedArticleRepository $relatedArticleRepository, + RelatedArticlesRepository $relatedArticlesRepository, + AbstractQuery $query, + AbstractQuery $getRelatedArticleQuery, + AbstractQuery $getAllArticlesQuery, + \Doctrine\DBAL\Connection $connection + + ){ + $article->getNumber()->willReturn(1); + $articleToRemove->getNumber()->willReturn(2); + $articleToAdd->getNumber()->willReturn(2); + $em->persist(Argument::any())->willReturn(true); + $em->flush(Argument::any())->willReturn(true); + $em->remove(Argument::any())->willReturn(true); + $em->getConnection()->willReturn($connection); + + $em->getRepository('Newscoop\Entity\RelatedArticle')->willReturn($relatedArticleRepository); + $em->getRepository('Newscoop\Entity\RelatedArticles')->willReturn($relatedArticlesRepository); + + $relatedArticlesRepository->getRelatedArticles(Argument::type('integer'))->willReturn($query); + + $relatedArticleRepository->getAllArticles(Argument::type('\Newscoop\Entity\RelatedArticles'))->willReturn($getAllArticlesQuery); + $getAllArticlesQuery->getResult()->willReturn(array()); + $relatedArticleRepository->getRelatedArticle(Argument::type('\Newscoop\Entity\RelatedArticles'), Argument::type('integer'))->willReturn($query); + $relatedArticleRepository->getRelatedArticle(Argument::type('integer'))->willReturn($getRelatedArticleQuery); + $getRelatedArticleQuery->getOneOrNullResult()->willReturn($relatedArticle); + + $relatedArticle->getOrder()->willReturn(Argument::type('integer')); + + $this->beConstructedWith($em); + } + function it_is_initializable() { $this->shouldHaveType('Newscoop\Services\RelatedArticlesService'); } - function it_should_get_related_articles() + function it_should_get_related_articles($article) + { + $this->getRelatedArticles($article); + } + + function it_should_remove_related_article($article, $articleToRemove) { - $this->getRelatedArticles(); + $this->removeRelatedArticle($article, $articleToRemove)->shouldReturn(true); } - function it_should_remove_related_article() + function it_should_add_related_article($article, $articleToAdd) { - $this->removeRelatedArticle(); + $this->addArticle($article, $articleToAdd); } - function it_should_add_related_article() + function it_should_reposition_related_article($article, $articleToAdd) { - $this->addArticle(); + $this->addArticle($article, $articleToAdd, 2); } }