Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Add data attributes to visit widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed Sep 21, 2018
1 parent f6958a4 commit 63788a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/PersonalRecentVisitsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Collection;
use Kontenta\Kontour\Contracts\PersonalRecentVisitsWidget as PersonalRecentVisitsWidgetContract;
use Kontenta\Kontour\Contracts\AdminLink;

class PersonalRecentVisitsWidget implements PersonalRecentVisitsWidgetContract
{
Expand All @@ -19,9 +17,9 @@ public function __construct(RecentVisitsRepository $repository)

public function toHtml()
{
return '<ul>'.$this->getVisits()->map(function ($visit) {
return '<li>'.$visit->getLink()->toHtml().'</li>';
})->implode("\n").'</ul>';
return '<ul>' . $this->getVisits()->map(function ($visit) {
return '<li data-kontour-visit-type="' . $visit->getType() . '">' . $visit->getLink()->toHtml() . '</li>';
})->implode("\n") . '</ul>';
}

public function isAuthorized(Authorizable $user = null): bool
Expand All @@ -31,7 +29,7 @@ public function isAuthorized(Authorizable $user = null): bool

private function getVisits()
{
return $this->repository->getShowVisits()->merge($this->repository->getEditVisits())->filter(function($visit) {
return $this->repository->getShowVisits()->merge($this->repository->getEditVisits())->filter(function ($visit) {
return $visit->getUser()->is(Auth::guard(config('kontour.guard'))->user());
})->unique(function ($visit) {
return $visit->getLink()->getUrl();
Expand Down
10 changes: 4 additions & 6 deletions src/TeamRecentVisitsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Collection;
use Kontenta\Kontour\Contracts\TeamRecentVisitsWidget as TeamRecentVisitsWidgetContract;
use Kontenta\Kontour\Contracts\AdminLink;

class TeamRecentVisitsWidget implements TeamRecentVisitsWidgetContract
{
Expand All @@ -19,9 +17,9 @@ public function __construct(RecentVisitsRepository $repository)

public function toHtml()
{
return '<ul>'.$this->getVisits()->map(function ($visit) {
return '<li data-kontour-username="'.$visit->getUser()->getDisplayName().'">'.$visit->getLink()->toHtml().'</li>';
})->implode("\n").'</ul>';
return '<ul>' . $this->getVisits()->map(function ($visit) {
return '<li data-kontour-visit-type="' . $visit->getType() . '" data-kontour-username="' . $visit->getUser()->getDisplayName() . '">' . $visit->getLink()->toHtml() . '</li>';
})->implode("\n") . '</ul>';
}

public function isAuthorized(Authorizable $user = null): bool
Expand All @@ -31,7 +29,7 @@ public function isAuthorized(Authorizable $user = null): bool

private function getVisits()
{
return $this->repository->getEditVisits()->filter(function($visit) {
return $this->repository->getEditVisits()->filter(function ($visit) {
return !$visit->getUser()->is(Auth::guard(config('kontour.guard'))->user());
})->unique(function ($visit) {
return $visit->getLink()->getUrl();
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/UserlandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Kontenta\KontourSupport\Tests\Feature;

use Illuminate\Support\Facades\Event;
use Kontenta\KontourSupport\Tests\UserlandAdminToolTest;
use Kontenta\KontourSupport\Tests\Feature\Fakes\User;
use Kontenta\Kontour\Events\AdminToolVisited;
use Kontenta\KontourSupport\AdminLink;
use Kontenta\KontourSupport\Tests\Feature\Fakes\User;
use Kontenta\KontourSupport\Tests\UserlandAdminToolTest;
use Kontenta\Kontour\EditAdminVisit;
use Kontenta\Kontour\Events\AdminToolVisited;
use Kontenta\Kontour\ShowAdminVisit;

class UserlandControllerTest extends UserlandAdminToolTest
Expand All @@ -33,13 +33,13 @@ public function test_index_route()
$response->assertSee('<main');
$response->assertSee('UserlandAdminWidget');
$response->assertDontSee('UnauthorizedWidget');
$response->assertSee('<a href="'.route('userland.index').'">Userland Tool</a>');
$response->assertSee('<a href="' . route('userland.index') . '">Userland Tool</a>');
$response->assertSee('>main<');
Event::assertDispatched(AdminToolVisited::class, function($e) {
Event::assertDispatched(AdminToolVisited::class, function ($e) {
$now = new \DateTimeImmutable();
return $e->visit->getLink()->getUrl() == route('userland.index') and
$this->user->is($e->visit->getUser()) and
$now->getTimestamp() - $e->visit->getDateTime()->getTimestamp() >= 0;
$this->user->is($e->visit->getUser()) and
$now->getTimestamp() - $e->visit->getDateTime()->getTimestamp() >= 0;
});
}

Expand All @@ -63,16 +63,16 @@ public function test_recent_visits_widgets()
$response->assertOk();

// Check personal links
$numberOfMatches = substr_count($response->content(), '<li><a href="'.route('userland.index').'">Recent Userland Tool</a>');
$numberOfMatches = substr_count($response->content(), '<li data-kontour-visit-type="show"><a href="' . route('userland.index') . '">Recent Userland Tool</a>');
$this->assertEquals(1, $numberOfMatches);

$numberOfMatches = substr_count($response->content(), '<li><a href="'.route('userland.edit').'">Recent Userland Tool</a>');
$numberOfMatches = substr_count($response->content(), '<li data-kontour-visit-type="edit"><a href="' . route('userland.edit') . '">Recent Userland Tool</a>');
$this->assertEquals(1, $numberOfMatches);

// Check team links
$numberOfMatches = substr_count($response->content(), '<li data-kontour-username="'.$otherUser->getDisplayName().'"><a href="'.route('userland.edit').'">Other Recent Userland Tool</a>');
$numberOfMatches = substr_count($response->content(), '<li data-kontour-visit-type="edit" data-kontour-username="' . $otherUser->getDisplayName() . '"><a href="' . route('userland.edit') . '">Other Recent Userland Tool</a>');
$this->assertEquals(1, $numberOfMatches);

$response->assertDontSee('<a href="'.route('userland.index').'">Other Recent Userland Tool</a>');
$response->assertDontSee('<a href="' . route('userland.index') . '">Other Recent Userland Tool</a>');
}
}

0 comments on commit 63788a1

Please sign in to comment.