Skip to content

Commit

Permalink
add spec and behat test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilles107 committed Jan 29, 2015
1 parent cb9ae0b commit 6740a15
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 17 deletions.
3 changes: 1 addition & 2 deletions 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
Expand Down Expand Up @@ -44,7 +44,6 @@ Feature: Endpoints
| fields[content] | <<sentence>> | 35 |

When I submit "article" data to "<<new_article>>"
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"
Expand Down
1 change: 0 additions & 1 deletion features/3_api_articles_search.feature
Expand Up @@ -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"
94 changes: 94 additions & 0 deletions 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 | <<sentence>> | 4 |
| language | 1 ||
| publication | 1 ||
| issue | ||
| section | ||
| comments_enabled | 1 ||
| type | news ||
| onFrontPage | 0 ||
| onSection | 0 ||
| keywords | <<text>> | 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 | <<sentence>> | 4 |
| language | 1 ||
| publication | 1 ||
| issue | ||
| section | ||
| comments_enabled | 1 ||
| type | news ||
| onFrontPage | 0 ||
| onSection | 0 ||
| keywords | <<text>> | 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 | <<sentence>> | 4 |
| language | 1 ||
| publication | 1 ||
| issue | ||
| section | ||
| comments_enabled | 1 ||
| type | news ||
| onFrontPage | 0 ||
| onSection | 0 ||
| keywords | <<text>> | 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 "<<base_article>>"
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 "<<base_article>>"
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 "<<base_article>>"
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 "<<base_article>>"
Then the response status code should be 204
And the response is JSON
6 changes: 3 additions & 3 deletions newscoop/library/Newscoop/Services/ArticleService.php
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion newscoop/library/Newscoop/Services/UserService.php
Expand Up @@ -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)) {
Expand Down
Expand Up @@ -404,7 +404,7 @@ public function getArticleAction(Request $request, $number)
* **related articles headers**:
*
* header name: "link"
* header value: "</api/article/1; rel="topic">"
* header value: "</api/article/1; rel="article">"
* or with specific language
*
* header value: "</api/article/1?language=en; rel="article">"
Expand Down
64 changes: 58 additions & 6 deletions spec/Newscoop/Services/RelatedArticlesServiceSpec.php
Expand Up @@ -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);
}
}

0 comments on commit 6740a15

Please sign in to comment.