]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Keep url in exists endpoint
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
index 8d96d7b8c67a28992607940278903a9ed57d6ead..fc4dc9d9218cdd8886526e2dcd41aa9103b274b6 100644 (file)
@@ -971,40 +971,42 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
     }
 
-    public function testGetEntriesExistsWithReturnId()
+    public function dataForEntriesExistWithUrl()
     {
-        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1');
+        $url = hash('md5', 'http://0.0.0.0/entry2');
 
-        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
-
-        $content = json_decode($this->client->getResponse()->getContent(), true);
-
-        // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
-        $this->assertGreaterThan(1, $content['exists']);
-    }
-
-    public function testGetEntriesExistsWithoutReturnId()
-    {
-        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
-
-        $this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
-
-        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
-
-        $content = json_decode($this->client->getResponse()->getContent(), true);
-
-        $this->assertTrue($content['exists']);
+        return [
+            'with_id' => [
+                'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
+                'expectedValue' => 2,
+            ],
+            'without_id' => [
+                'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
+                'expectedValue' => true,
+            ],
+            'hashed_url_with_id' => [
+                'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1',
+                'expectedValue' => 2,
+            ],
+            'hashed_url_without_id' => [
+                'url' => '/api/entries/exists?hashed_url=' . $url . '',
+                'expectedValue' => true,
+            ],
+        ];
     }
 
-    public function testGetEntriesExistsWithHash()
+    /**
+     * @dataProvider dataForEntriesExistWithUrl
+     */
+    public function testGetEntriesExists($url, $expectedValue)
     {
-        $this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
+        $this->client->request('GET', $url);
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertSame(2, $content['exists']);
+        $this->assertSame($expectedValue, $content['exists']);
     }
 
     public function testGetEntriesExistsWithManyUrls()
@@ -1045,42 +1047,37 @@ class EntryRestControllerTest extends WallabagApiTestCase
     {
         $url1 = 'http://0.0.0.0/entry2';
         $url2 = 'http://0.0.0.0/entry10';
-        $this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2) . '&return_id=1');
+        $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('md5', $url1) . '&hashed_urls[]=' . hash('md5', $url2) . '&return_id=1');
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertArrayHasKey($url1, $content);
-        $this->assertArrayHasKey($url2, $content);
-        $this->assertSame(2, $content[$url1]);
-        $this->assertNull($content[$url2]);
-
         $this->assertArrayHasKey(hash('md5', $url1), $content);
         $this->assertArrayHasKey(hash('md5', $url2), $content);
-        $this->assertEquals(2, $content[hash('md5', $url1)]);
-        $this->assertEquals(false, $content[hash('md5', $url2)]);
+        $this->assertSame(2, $content[hash('md5', $url1)]);
+        $this->assertNull($content[hash('md5', $url2)]);
     }
 
     public function testGetEntriesExistsWithManyUrlsHashedReturnBool()
     {
         $url1 = 'http://0.0.0.0/entry2';
         $url2 = 'http://0.0.0.0/entry10';
-        $this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2));
+        $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('md5', $url1) . '&hashed_urls[]=' . hash('md5', $url2));
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertArrayHasKey($url1, $content);
-        $this->assertArrayHasKey($url2, $content);
-        $this->assertTrue($content[$url1]);
-        $this->assertFalse($content[$url2]);
+        $this->assertArrayHasKey(hash('md5', $url1), $content);
+        $this->assertArrayHasKey(hash('md5', $url2), $content);
+        $this->assertTrue($content[hash('md5', $url1)]);
+        $this->assertFalse($content[hash('md5', $url2)]);
     }
 
     public function testGetEntriesExistsWhichDoesNotExists()
     {
-        $this->client->request('GET', '/api/entries/exists?hashedurl='.hash('md5','http://google.com/entry2'));
+        $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('md5', 'http://google.com/entry2'));
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
@@ -1091,7 +1088,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
     public function testGetEntriesExistsWithNoUrl()
     {
-        $this->client->request('GET', '/api/entries/exists?hashedurl=');
+        $this->client->request('GET', '/api/entries/exists?hashed_url=');
 
         $this->assertSame(403, $this->client->getResponse()->getStatusCode());
     }