Use only one method to randomize

Instead of one per type, one for all is ok.
This commit is contained in:
Jeremy Benoist 2018-10-12 21:41:05 +02:00
parent f85d220c19
commit 0447a75b06
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
4 changed files with 42 additions and 85 deletions

View 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
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function showRandomUnreadEntryAction()
public function redirectRandomEntryAction($type = 'all')
{
return $this->showRandomEntries('unread');
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');
return $this->redirect($this->generateUrl('homepage'));
}
/**
* 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
*/
public function showRandomArchiveEntryAction()
{
return $this->showRandomEntries('archive');
}
/**
* 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');
}
/**
* 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.

View 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 %}

View 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 %}

View 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');
}
}