From 6a700f784c07c782dace71bf9540914a30b4a0b7 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 28 Oct 2017 10:52:38 -0400 Subject: [PATCH] Tests for TTRSS operation setArticleLabel; fixes #90 --- lib/REST/TinyTinyRSS/API.php | 4 +-- tests/REST/TinyTinyRSS/TestTinyTinyAPI.php | 38 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/REST/TinyTinyRSS/API.php b/lib/REST/TinyTinyRSS/API.php index 8fac3a1..8ea00e5 100644 --- a/lib/REST/TinyTinyRSS/API.php +++ b/lib/REST/TinyTinyRSS/API.php @@ -643,10 +643,10 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { public function opSetArticleLabel(array $data): array { $label = $this->labelIn($data['label_id']); - $articles = explode(",", $data['article_ids']); + $articles = explode(",", (string) $data['article_ids']); $assign = $data['assign'] ?? false; $out = 0; - $in = array_chunk($data['article_ids'], 50); + $in = array_chunk($articles, 50); for ($a = 0; $a < sizeof($in); $a++) { // initialize the matching context $c = new Context; diff --git a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php index 822473c..e208c8c 100644 --- a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php +++ b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php @@ -824,4 +824,42 @@ class TestTinyTinyAPI extends Test\AbstractTest { $this->assertResponse($this->respGood($exp[$a]), $this->h->dispatch(new Request("POST", "", json_encode($in[$a]))), "Test $a failed"); } } + + public function testAssignArticlesToALabel() { + $list = [ + range(1,100), + range(1,50), + range(51,100), + ]; + $in = [ + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => -2112, 'article_ids' => implode(",", $list[0])], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => -2112, 'article_ids' => implode(",", $list[0]), 'assign' => true], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => -2112], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => -42], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => 42], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx", 'label_id' => 0], + ['op' => "setArticleLabel", 'sid' => "PriestsOfSyrinx"], + ]; + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, $this->anything(), (new Context)->articles([]), $this->anything())->thenThrow(new ExceptionInput("tooShort")); // data model function requires one valid integer for multiples + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, $this->anything(), (new Context)->articles($list[0]), $this->anything())->thenThrow(new ExceptionInput("tooLong")); // data model function limited to 50 items for multiples + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[1]), true)->thenReturn(42); + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[2]), true)->thenReturn(47); + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[1]), false)->thenReturn(5); + Phake::when(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[2]), false)->thenReturn(2); + $exp = $this->respGood(['status' => "OK", 'updated' => 89]); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[0])))); + Phake::verify(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[1]), true); + Phake::verify(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[2]), true); + $exp = $this->respGood(['status' => "OK", 'updated' => 7]); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[1])))); + Phake::verify(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[1]), false); + Phake::verify(Arsse::$db)->labelArticlesSet(Arsse::$user->id, 1088, (new Context)->articles($list[2]), false); + $exp = $this->respGood(['status' => "OK", 'updated' => 89]); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[2])))); + $exp = $this->respErr("INCORRECT_USAGE"); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[3])))); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[4])))); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[5])))); + $this->assertResponse($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[6])))); + } }