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

Commit

Permalink
Merge pull request #15 from kontenta/item-history
Browse files Browse the repository at this point in the history
Item history
  • Loading branch information
bjuppa authored Oct 3, 2018
2 parents f354a9a + 45b06fc commit f41c3f4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
14 changes: 14 additions & 0 deletions resources/views/widgets/itemHistory.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<section data-kontour-widget="itemHistory">
<header>Item History</header>
<ul>
@foreach($entries as $entry)
<li lang="en" data-kontour-entry-action="{{ $entry['action'] }}"@if($entry['user']) data-kontour-username="{{ $entry['user']->getDisplayName() }}"@endif>
<span>{{ ucfirst($entry['action']) }}</span>
<time datetime="{{ $entry['datetime']->toDateTimeString() }}">{{ $entry['datetime']->diffForHumans() }}</time>
@if($entry['user'])
<span>by</span> <span>{{ $entry['user']->getDisplayName() }}</span>
@endif
</li>
@endforeach
</ul>
</section>
7 changes: 7 additions & 0 deletions src/Providers/KontourServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Kontenta\KontourSupport\AdminWidgetManager;
use Kontenta\KontourSupport\Widgets\MenuWidget;
use Kontenta\KontourSupport\RecentVisitsRepository;
use Kontenta\KontourSupport\Widgets\ItemHistoryWidget;
use Kontenta\KontourSupport\Widgets\PersonalRecentVisitsWidget;
use Kontenta\KontourSupport\Widgets\TeamRecentVisitsWidget;
use Kontenta\KontourSupport\Http\Middleware\AuthenticateAdmin;
Expand Down Expand Up @@ -93,6 +94,12 @@ function ($app) {
TeamRecentVisitsWidget::class,
true
);

$this->app->bindIf(
\Kontenta\Kontour\Contracts\ItemHistoryWidget::class,
ItemHistoryWidget::class,
true
);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Widgets/ItemHistoryWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Kontenta\KontourSupport\Widgets;

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Support\Collection;
use Kontenta\Kontour\Contracts\ItemHistoryWidget as ItemHistoryWidgetContract;
use Kontenta\Kontour\Contracts\AdminUser;
use Illuminate\Support\Facades\View;
use Carbon\Carbon;

class ItemHistoryWidget implements ItemHistoryWidgetContract
{
/**
* @var Collection
*/
protected $entries;

public function __construct()
{
$this->entries = new Collection();
}

public function toHtml()
{
return View::make('kontour::widgets.itemHistory', ['entries' => $this->entries])->render();
}

public function addEntry(string $action, \DateTime $datetime, AdminUser $user = null): ItemHistoryWidgetContract
{
$datetime = Carbon::instance($datetime);
$this->entries->push(compact('action', 'datetime', 'user'));
return $this;
}

public function addCreatedEntry(\DateTime $datetime, AdminUser $user = null): ItemHistoryWidgetContract
{
return $this->addEntry('created', $datetime, $user);
}

public function addUpdatedEntry(\DateTime $datetime, AdminUser $user = null): ItemHistoryWidgetContract
{
return $this->addEntry('updated', $datetime, $user);
}

public function isAuthorized(Authorizable $user = null): bool
{
return true;
}
}
11 changes: 10 additions & 1 deletion tests/Feature/Fakes/UserlandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
use Kontenta\Kontour\Events\AdminToolVisited;
use Kontenta\KontourSupport\AdminLink;
use Kontenta\Kontour\ShowAdminVisit;
use Kontenta\Kontour\Concerns\RegistersAdminWidgets;
use Kontenta\Kontour\Contracts\ItemHistoryWidget;

class UserlandController extends BaseController
{
use RegistersAdminWidgets;

public function index()
{
$link = new AdminLink(url()->full(), 'Recent Userland Tool');
Expand All @@ -36,7 +40,12 @@ public function show($id)

public function edit($id)
{
//
$widget = app(ItemHistoryWidget::class);
$this->registerAdminWidget($widget);
$widget->addCreatedEntry(new \DateTime(), Auth::guard(config('kontour.guard'))->user());
$widget->addUpdatedEntry(new \DateTime(), Auth::guard(config('kontour.guard'))->user());

return view('userland::index');
}

public function update($id)
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Fakes/UserlandServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function registerRoutes()
'namespace' => 'Kontenta\KontourSupport\Tests\Feature\Fakes',
], function ($router) {
$router->get('/', 'UserlandController@index')->name('userland.index');
$router->get('edit', 'UserlandController@edit')->name('userland.edit');
$router->get('edit/{id}', 'UserlandController@edit')->name('userland.edit');
});
});
}
Expand Down
16 changes: 12 additions & 4 deletions tests/Feature/UserlandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public function test_index_route()
public function test_recent_visits_widgets()
{
$otherUser = factory(User::class)->create();
$link = new AdminLink(route('userland.edit'), 'Recent Userland Tool');
$link = new AdminLink(route('userland.edit', 1), 'Recent Userland Tool');
$visit = new EditAdminVisit($link, $this->user);
event(new AdminToolVisited($visit));
event(new AdminToolVisited($visit));
$link = new AdminLink(route('userland.index'), 'Other Recent Userland Tool');
$visit = new ShowAdminVisit($link, $otherUser);
event(new AdminToolVisited($visit));
$link = new AdminLink(route('userland.edit'), 'Other Recent Userland Tool');
$link = new AdminLink(route('userland.edit', 1), 'Other Recent Userland Tool');
$visit = new EditAdminVisit($link, $otherUser);
event(new AdminToolVisited($visit));
event(new AdminToolVisited($visit));
Expand All @@ -69,16 +69,24 @@ public function test_recent_visits_widgets()
$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 data-kontour-visit-type="edit"><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', 1) . '">Recent Userland Tool</a>');
$this->assertEquals(1, $numberOfMatches);

// Check team links
$response->assertSee('<aside data-kontour-widget="teamRecentVisits">');
$response->assertSee('<header>Team Recent</header>');

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

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

public function test_item_history_widget()
{
$response = $this->actingAs($this->user)->get(route('userland.edit', 1));
$response->assertSee('<section data-kontour-widget="itemHistory">');
$response->assertSee('<li lang="en" data-kontour-entry-action="created" data-kontour-username="'.$this->user->getDisplayName() .'">');
$response->assertSee('<li lang="en" data-kontour-entry-action="updated" data-kontour-username="'.$this->user->getDisplayName() .'">');
}
}

0 comments on commit f41c3f4

Please sign in to comment.