]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Use only one method to randomize
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 12 Oct 2018 19:41:05 +0000 (21:41 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 19 Jan 2019 20:09:33 +0000 (21:09 +0100)
Instead of one per type, one for all is ok.

src/Wallabag/CoreBundle/Controller/EntryController.php
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig
tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php

index 669e15d7ca95262cd5fe49882ecbe2bd3c108c24..ab50ebdfdf7550f82f09a83e2bf74d64743c3e66 100644 (file)
@@ -249,73 +249,28 @@ class EntryController extends Controller
     }
 
     /**
-     * Shows random unread entry.
+     * Shows random entry depending on the given type.
      *
      * @param Entry $entry
      *
-     * @Route("/unread/random", name="unread_random")
+     * @Route("/{type}/random", name="random_entry", requirements={"_locale": "unread|starred|archive|untagged|all"})
      *
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    public function showRandomUnreadEntryAction()
-    {
-        return $this->showRandomEntries('unread');
-    }
-
-    /**
-     * Shows random favorite entry.
-     *
-     * @param Entry $entry
-     *
-     * @Route("/starred/random", name="starred_random")
-     *
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    public function showRandomStarredEntryAction()
-    {
-        return $this->showRandomEntries('starred');
-    }
-
-    /**
-     * Shows random archived entry.
-     *
-     * @param Entry $entry
-     *
-     * @Route("/archive/random", name="archive_random")
-     *
-     * @return \Symfony\Component\HttpFoundation\Response
+     * @return \Symfony\Component\HttpFoundation\RedirectResponse
      */
-    public function showRandomArchiveEntryAction()
+    public function redirectRandomEntryAction($type = 'all')
     {
-        return $this->showRandomEntries('archive');
-    }
+        try {
+            $entry = $this->get('wallabag_core.entry_repository')
+                ->getRandomEntry($this->getUser()->getId(), $type);
+        } catch (NoResultException $e) {
+            $bag = $this->get('session')->getFlashBag();
+            $bag->clear();
+            $bag->add('notice', 'flashes.entry.notice.no_random_entry');
 
-    /**
-     * Shows random all entry.
-     *
-     * @param Entry $entry
-     *
-     * @Route("/untagged/random", name="untagged_random")
-     *
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    public function showRandomUntaggedEntryAction()
-    {
-        return $this->showRandomEntries('untagged');
-    }
+            return $this->redirect($this->generateUrl('homepage'));
+        }
 
-    /**
-     * Shows random all entry.
-     *
-     * @param Entry $entry
-     *
-     * @Route("/all/random", name="all_random")
-     *
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    public function showRandomAllEntryAction()
-    {
-        return $this->showRandomEntries();
+        return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
     }
 
     /**
@@ -623,30 +578,6 @@ class EntryController extends Controller
         );
     }
 
-    /**
-     * Global method to retrieve random entries depending on the given type.
-     *
-     * @param string $type Entries type: unread, starred, archive or untagged
-     *
-     * @return \Symfony\Component\HttpFoundation\RedirectResponse
-     */
-    private function showRandomEntries($type)
-    {
-        $repository = $this->get('wallabag_core.entry_repository');
-
-        try {
-            $entry = $repository->getRandomEntry($this->getUser()->getId(), $type);
-        } catch (NoResultException $e) {
-            $bag = $this->get('session')->getFlashBag();
-            $bag->clear();
-            $bag->add('notice', 'flashes.entry.notice.no_random_entry');
-
-            return $this->redirect($this->generateUrl('homepage'));
-        }
-
-        return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
-    }
-
     /**
      * Fetch content and update entry.
      * In case it fails, $entry->getContent will return an error message.
index 7108efbd430bd3c929f55499e86796ef5dee8ddd..f3baae2c7c78e944a959e20e9b4d5ca9809e6981 100644 (file)
@@ -28,7 +28,7 @@
         <div class="nb-results">{{ 'entry.list.number_on_the_page'|transchoice(entries.count) }}</div>
         <div class="pagination">
             <a href="{{ path('switch_view_mode') }}"><i class="listMode-btn material-icons md-24">{% if listMode == 0 %}list{% else %}view_module{% endif %}</i></a>
-            <a href="{{ path(currentRoute ~ '_random') }}">random</a>
+            <a href="{{ path('random_entry', { 'type': currentRoute }) }}">random</a>
             {% if app.user.config.rssToken %}
                 {% include "@WallabagCore/themes/common/Entry/_rss_link.html.twig" %}
             {% endif %}
index 5deda0fc8a3849f823a36d7f7a479847b76fe6d1..31c4b5cbecf8b17509729f7b5cbc65ceb2df5ce0 100644 (file)
@@ -28,7 +28,7 @@
         <div class="nb-results">
             {{ 'entry.list.number_on_the_page'|transchoice(entries.count) }}
             <a href="{{ path('switch_view_mode') }}"><i class="material-icons">{% if listMode == 0 %}view_list{% else %}view_module{% endif %}</i></a>
-            <a href="{{ path(currentRoute ~ '_random') }}">random</a>
+            <a href="{{ path('random_entry', { 'type': currentRoute }) }}">random</a>
             {% if app.user.config.rssToken %}
                 {% include "@WallabagCore/themes/common/Entry/_rss_link.html.twig" %}
             {% endif %}
index 005296ff33572c742db613f845289a37f7d43f19..28291b5a029825b49012e0bb6bcd160c77331e09 100644 (file)
@@ -1495,4 +1495,30 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $this->assertSame(sprintf('/remove-tag/%s/%s', $entry->getId(), $tag->getId()), $link);
     }
+
+    public function testRandom()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->request('GET', '/unread/random');
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Unread random');
+
+        $client->request('GET', '/starred/random');
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Starred random');
+
+        $client->request('GET', '/archive/random');
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Archive random');
+
+        $client->request('GET', '/untagged/random');
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Untagged random');
+
+        $client->request('GET', '/all/random');
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'All random');
+    }
 }