Skip to content

Commit

Permalink
Merge pull request #33 from kontenta/adminlinks
Browse files Browse the repository at this point in the history
Admin links rework
  • Loading branch information
erik-epineer authored Oct 19, 2018
2 parents b1a28c7 + a21bb14 commit 73ebfc6
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 40 deletions.
25 changes: 21 additions & 4 deletions src/AdminLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ class AdminLink implements AdminLinkContract, AuthorizesWithAbilityContract
{
use AuthorizesWithAbilityTrait;

protected $url;
protected $name;
protected $url;
protected $description;

public function __construct(string $url, string $name, string $description = null)
public function __construct(string $name, string $url, string $description = null)
{
$this->url = $url;
$this->name = $name;
$this->description = $description;
$this->url = $url;
$this->withDescription($description);
}

public static function create(...$args): AdminLink
{
return new static(...$args);
}

public function getUrl(): ?string
Expand All @@ -36,6 +41,18 @@ public function getDescription(): ?string
return $this->description;
}

/**
* Set description fluently
* @param string $description
* @return $this
*/
public function withDescription(string $description = null): AdminLinkContract
{
$this->description = $description;

return $this;
}

public function toHtml(): string
{
$attributes = [];
Expand Down
18 changes: 9 additions & 9 deletions src/Concerns/RegistersMenuWidgetLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace Kontenta\Kontour\Concerns;

use Kontenta\Kontour\AdminLink as UrlAdminLink;
use Kontenta\Kontour\Contracts\AdminLink;
use Kontenta\Kontour\AdminLink;
use Kontenta\Kontour\Contracts\AdminLink as AdminLinkContract;
use Kontenta\Kontour\Contracts\MenuWidget;
use Kontenta\Kontour\RouteAdminLink;

trait RegistersMenuWidgetLinks
{
public function addMenuWidgetUrl(string $url, string $name, string $description = null, string $desiredHeading = null)
public function addMenuWidgetUrl(string $name, string $url, string $desiredHeading = null): AdminLink
{
$link = new UrlAdminLink($url, $name, $description);
$this->addMenuWidgetAdminLink($link, $desiredHeading);
return $this->addMenuWidgetAdminLink(AdminLink::create($name, $url), $desiredHeading);
}

public function addMenuWidgetRoute(string $routeName, string $name, string $description = null, string $desiredHeading = null)
public function addMenuWidgetRoute(string $name, string $routeName, $routeParameters = [], string $desiredHeading = null): RouteAdminLink
{
$link = new RouteAdminLink($routeName, $name, $description);
$this->addMenuWidgetAdminLink($link, $desiredHeading);
return $this->addMenuWidgetAdminLink(RouteAdminLink::create($name, $routeName, $routeParameters), $desiredHeading);
}

public function addMenuWidgetAdminLink(AdminLink $link, string $desiredHeading = null)
public function addMenuWidgetAdminLink(AdminLinkContract $link, string $desiredHeading = null): AdminLinkContract
{
$this->resolveMenuWidget()->addLink($link, $desiredHeading);

return $link;
}

protected function resolveMenuWidget(): MenuWidget
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/AdminLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ public function getUrl(): ?string;
public function getName(): string;

public function getDescription(): ?string;

/**
* Set description fluently
* @param string $description
* @return $this
*/
public function withDescription(string $description = null): AdminLink;
}
27 changes: 22 additions & 5 deletions src/RouteAdminLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@

namespace Kontenta\Kontour;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;

class RouteAdminLink extends AdminLink
{
protected $routeName;
protected $routeParameters;

public function __construct(string $routeName, string $name, string $description = null)
public function __construct(string $name, string $routeName, $routeParameters = [], string $description = null)
{
$this->routeName = $routeName;
$this->name = $name;
$this->description = $description;
$this->routeName = $routeName;
$this->withRouteParameters($routeParameters);
$this->withDescription($description);
}

/**
* Set route parameters fluently
* @param mixed $routeParameters
* @return $this
*/
public function withRouteParameters($routeParameters = []): RouteAdminLink
{
$this->routeParameters = $routeParameters;

return $this;
}

public function getUrl(): ?string
{
return Route::has($this->routeName) ? URL::route($this->routeName) : null;
try {
return URL::route($this->routeName, $this->routeParameters);
} catch (\Exception $e) {
return null;
}
}
}
15 changes: 7 additions & 8 deletions tests/Feature/Fakes/UserlandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Kontenta\Kontour\Events\AdminToolVisited;
use Kontenta\Kontour\AdminLink;
use Kontenta\Kontour\RouteAdminLink;
use Kontenta\Kontour\ShowAdminVisit;
use Kontenta\Kontour\Concerns\RegistersAdminWidgets;
use Kontenta\Kontour\Contracts\ItemHistoryWidget;
use Kontenta\Kontour\Contracts\CrumbtrailWidget;
use Kontenta\Kontour\Contracts\ItemHistoryWidget;
use Kontenta\Kontour\Contracts\MessageWidget;
use Kontenta\Kontour\Events\AdminToolVisited;
use Kontenta\Kontour\RouteAdminLink;
use Kontenta\Kontour\ShowAdminVisit;

class UserlandController extends BaseController
{
Expand All @@ -20,14 +20,13 @@ class UserlandController extends BaseController
public function __construct(CrumbtrailWidget $crumbtrail)
{
$this->crumbtrail = $crumbtrail;
$link1 = new RouteAdminLink('userland.index', '1');
$link1 = new RouteAdminLink('1', 'userland.index');
$this->crumbtrail->addLink($link1);
}


public function index()
{
$link = new AdminLink(url()->full(), 'Recent Userland Tool');
$link = new AdminLink('Recent Userland Tool', url()->full());
$user = Auth::guard(config('kontour.guard'))->user();
$visit = new ShowAdminVisit($link, $user);
event(new AdminToolVisited($visit));
Expand Down Expand Up @@ -56,7 +55,7 @@ public function edit($id)
$widget->addCreatedEntry(new \DateTime(), Auth::guard(config('kontour.guard'))->user());
$widget->addUpdatedEntry(new \DateTime(), Auth::guard(config('kontour.guard'))->user());

$link2 = new AdminLink(url()->full(), '2');
$link2 = new AdminLink('2', url()->full());
$this->crumbtrail->addLink($link2);
$this->registerAdminWidget($this->crumbtrail, app(\Kontenta\Kontour\Contracts\AdminViewManager::class)->toolHeaderSection());

Expand Down
5 changes: 2 additions & 3 deletions tests/Feature/Fakes/UserlandServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ protected function registerWidgets()

protected function registerMenuLinks()
{
$routeName = 'userland.index';
$name = 'Userland Tool';
$this->addMenuWidgetRoute($routeName, $name);
$this->addMenuWidgetRoute('Userland Tool', 'userland.index');
$this->addMenuWidgetUrl('External Link', 'http://external.com', 'External');
}
}
48 changes: 46 additions & 2 deletions tests/Feature/RouteAdminLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,55 @@

class RouteAdminLinkTest extends IntegrationTest
{
/**
* Configure the environment.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Test with Laravel's default password reset config
// This is to have the route kontour.password.reset requiring parameter token available
$app['config']->set('kontour.passwords', 'users');
parent::getEnvironmentSetUp($app);
}

public function test_url_is_null_when_route_does_not_exist()
{
$link = new RouteAdminLink('not.existing', 'Hej', '"Hejsanhejsan"');
$link = new RouteAdminLink('Gone', 'not.existing');

$this->assertNull($link->getUrl());
$this->assertEquals('<a >Gone</a>', $link->toHtml());
}

public function test_url_is_null_when_parameters_missing()
{
$link = new RouteAdminLink('Reset password', 'kontour.password.reset');

$this->assertNull($link->getUrl());
$this->assertEquals('<a title="&quot;Hejsanhejsan&quot;">Hej</a>', $link->toHtml());
}

public function test_route_parameters_can_be_added_in_constructor()
{
$link = new RouteAdminLink('Reset password', 'kontour.password.reset', ["token" => 'abc']);

$this->assertEquals(route('kontour.password.reset', ["token" => 'abc']), $link->getUrl());
}

public function test_route_parameters_can_be_added_fluently()
{
$link = new RouteAdminLink('Reset password', 'kontour.password.reset');
$link = $link->withRouteParameters(["token" => 'abc']);

$this->assertEquals(route('kontour.password.reset', ["token" => 'abc']), $link->getUrl());
}

public function test_can_be_created_statically()
{
$link = RouteAdminLink::create('Reset password', 'kontour.password.reset', ["token" => 'abc']);

$this->assertTrue($link instanceof RouteAdminLink);
$this->assertEquals(route('kontour.password.reset', ["token" => 'abc']), $link->getUrl());
}
}
16 changes: 9 additions & 7 deletions tests/Feature/UserlandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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

class UserlandControllerTest extends UserlandAdminToolTest
{
Expand Down Expand Up @@ -49,19 +49,21 @@ public function test_menu_widget()
$response->assertSee('<ul data-kontour-widget="menu">');
$response->assertSee('>main<');
$response->assertSee('<a href="' . route('userland.index') . '">Userland Tool</a>');
$response->assertSee('>External<');
$response->assertSee('<a href="http://external.com">External Link</a>');
}

public function test_recent_visits_widgets()
{
$otherUser = factory(User::class)->create();
$link = new AdminLink(route('userland.edit', 1), 'Recent Userland Tool');
$link = new AdminLink('Recent Userland Tool', route('userland.edit', 1));
$visit = new EditAdminVisit($link, $this->user);
event(new AdminToolVisited($visit));
event(new AdminToolVisited($visit));
$link = new AdminLink(route('userland.index'), 'Other Recent Userland Tool');
$link = new AdminLink('Other Recent Userland Tool', route('userland.index'));
$visit = new ShowAdminVisit($link, $otherUser);
event(new AdminToolVisited($visit));
$link = new AdminLink(route('userland.edit', 1), 'Other Recent Userland Tool');
$link = new AdminLink('Other Recent Userland Tool', route('userland.edit', 1));
$visit = new EditAdminVisit($link, $otherUser);
event(new AdminToolVisited($visit));
event(new AdminToolVisited($visit));
Expand Down Expand Up @@ -102,8 +104,8 @@ public function test_crumbtrail_widget()
{
$response = $this->actingAs($this->user)->get(route('userland.edit', 1));
$response->assertSee('<nav aria-label="Crumb trail" data-kontour-widget="crumbtrail">');
$response->assertSee('<a href="'.route('userland.index').'">1</a>');
$response->assertSee('<li aria-current="page"><a href="'.route('userland.edit', 1).'">2</a>');
$response->assertSee('<a href="' . route('userland.index') . '">1</a>');
$response->assertSee('<li aria-current="page"><a href="' . route('userland.edit', 1) . '">2</a>');
}

public function test_message_widget()
Expand Down
22 changes: 20 additions & 2 deletions tests/Unit/AdminLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@

namespace Kontenta\Kontour\Tests\Unit;

use Kontenta\Kontour\Tests\UnitTest;
use Kontenta\Kontour\AdminLink;
use Kontenta\Kontour\Tests\UnitTest;

class AdminLinkTest extends UnitTest
{
public function test_can_be_converted_to_html()
{
$link = new AdminLink('http://hej.com', 'Hej', '"Hejsanhejsan"');
$link = new AdminLink('Hej', 'http://hej.com', '"Hejsanhejsan"');

$this->assertEquals('<a href="http://hej.com" title="&quot;Hejsanhejsan&quot;">Hej</a>', $link->toHtml());
}

public function test_description_can_be_added_fluently()
{
$link = new AdminLink('Hej', 'http://hej.com');
$link = $link->withDescription('Hejsanhejsan');

$this->assertEquals('<a href="http://hej.com" title="Hejsanhejsan">Hej</a>', $link->toHtml());
}

public function test_can_be_created_statically()
{
$link = AdminLink::create('Hej', 'http://hej.com', 'Hejsanhejsan');

$this->assertTrue($link instanceof AdminLink);
$this->assertEquals('Hej', $link->getName());
$this->assertEquals('http://hej.com', $link->getUrl());
$this->assertEquals('Hejsanhejsan', $link->getDescription());
}
}

0 comments on commit 73ebfc6

Please sign in to comment.