diff --git a/app/Modules/Navigation/BackendController.php b/app/Modules/Navigation/BackendController.php index 626effe0..c90f1031 100644 --- a/app/Modules/Navigation/BackendController.php +++ b/app/Modules/Navigation/BackendController.php @@ -13,7 +13,7 @@ use App\Model\Menu; use Illuminate\Http\Request; use App\Modules\ModuleEngine; -use Illuminate\Validation\Rule; +use Illuminate\Http\RedirectResponse; use App\Classes\Repositories\LinkRepository; use App\Classes\Repositories\MenuRepository; use App\Classes\Repositories\PageRepository; @@ -80,13 +80,11 @@ public function create() * External or Internal Hyperlinks and connections. * * @param Menu $menu - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\RedirectResponse + * @param Request $request + * @return RedirectResponse */ public function store(Request $request, Menu $menu) { - $request->validate(['title' => 'min:3|max:255|unique:menus,title,NULL,id,deleted_at,NULL|required']); - $this->save($request, $menu); return redirect()->route('admin.navigation.index'); @@ -125,8 +123,6 @@ public function edit($id) */ public function update(Request $request, $id) { - $request->validate(['title' => ['min:3|max:255|required', Rule::unique('menus')->ignore($id)]]); - $menu = $this->menus->whereID($id); $this->save($request, $menu); @@ -185,7 +181,9 @@ public function reorder(Request $request) */ private function save(Request $request, Menu $menu) { - $this->validate($request, ['title => required|min:3|max:255']); + $this->validate($request, [ + 'title' => 'min:3|max:255|required|unique:menus,title,NULL,id,deleted_at,NULL', + ]); DB::transaction(function () use ($request, $menu) { if ($request['linkable_object']) { diff --git a/composer.json b/composer.json index c3c43004..34865086 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^2.0", - "phpunit/phpunit": "^7.0" + "phpunit/phpunit": "^7.0", + "ext-json": "*" }, "autoload": { "files": [ diff --git a/tests/Navigation/NavigationBackendTest.php b/tests/Navigation/NavigationBackendTest.php new file mode 100644 index 00000000..67a12fb3 --- /dev/null +++ b/tests/Navigation/NavigationBackendTest.php @@ -0,0 +1,67 @@ +signIn(); + + // create a menu that has been deleted. + $menu = factory(Menu::class)->create(['title' => 'foo', 'deleted_at' => Carbon::now()->timestamp]); + + // create a new page for the new menu + $new_page = factory(Page::class)->create(); + + // encode to json for creation of a new link model + $encoding = json_encode(['key' => $new_page->id, 'class' => $new_page->getMorphClass()]); + + // generate teh new menu with the encoding + $new_menu = factory(Menu::class)->make(['title' => 'foo', 'linkable_object' => $encoding]); + + // get a request from the dashboard once passing the model to the url. + $request = $this->post('/admin/navigation/', $new_menu->toArray()); + + // make sure the database has the new menu added. + $this->assertDatabaseHas('menus', ['title' => 'foo', 'page_id' => $new_menu->page->id]); + } + + /** + * @test + */ + public function it_can_change_the_title_to_an_already_used_title_that_was_removed() : void + { + $this->signIn(); + + // A menu that has been deleted. + $menu = factory(Menu::class)->create(['title' => 'foo', 'deleted_at' => Carbon::now()->timestamp]); + + // the current menu to be changed + /** @var Menu $current_menu */ + $current_menu = factory(Menu::class)->create(['title' => 'bar']); + + // set its title back to foo. + $current_menu->setAttribute('title', 'foo'); + + // send the new menu as a + $request = $this->patch("/admin/navigation/{$current_menu->id}", $current_menu->toArray()); + + // assert the database has the changed details. + $this->assertDatabaseHas('menus', ['title' => 'foo', 'id' => $current_menu->getKey()]); + } +} diff --git a/tests/Common/NavigationTest.php b/tests/Navigation/NavigationModelTest.php similarity index 95% rename from tests/Common/NavigationTest.php rename to tests/Navigation/NavigationModelTest.php index 3f96d910..46737533 100644 --- a/tests/Common/NavigationTest.php +++ b/tests/Navigation/NavigationModelTest.php @@ -7,7 +7,7 @@ /** * Class NavigationTest. */ -class NavigationTest extends TestCase +class NavigationModelTest extends TestCase { /** * @test