Merge remote-tracking branch 'origin/Page_Issue' into development
This commit is contained in:
@@ -46,7 +46,7 @@ class ArticleController extends Controller
|
|||||||
// checking authentication
|
// checking authentication
|
||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
// checking roles
|
// checking roles
|
||||||
$this->middleware('roles');
|
$this->middleware('role.agent');
|
||||||
SettingsController::language();
|
SettingsController::language();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -40,7 +40,7 @@ class CategoryController extends Controller
|
|||||||
// checking authentication
|
// checking authentication
|
||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
// checking roles
|
// checking roles
|
||||||
$this->middleware('roles');
|
$this->middleware('role.agent');
|
||||||
SettingsController::language();
|
SettingsController::language();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,9 +150,9 @@ class CategoryController extends Controller
|
|||||||
try {
|
try {
|
||||||
$category->fill($request->input())->save();
|
$category->fill($request->input())->save();
|
||||||
|
|
||||||
return Redirect::back()->with('success', Lang::get('lang.category_inserted_successfully'));
|
return redirect('category')->with('success', Lang::get('lang.category_inserted_successfully'));
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
return Redirect::back()->with('fails', Lang::get('lang.category_not_inserted').'<li>'.$e->getMessage().'</li>');
|
return redirect('category')->with('fails', Lang::get('lang.category_not_inserted').'<li>'.$e->getMessage().'</li>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -38,7 +38,7 @@ class PageController extends Controller
|
|||||||
// checking authentication
|
// checking authentication
|
||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
// checking roles
|
// checking roles
|
||||||
$this->middleware('roles');
|
$this->middleware('role.agent');
|
||||||
$this->page = $page;
|
$this->page = $page;
|
||||||
SettingsController::language();
|
SettingsController::language();
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ class PageController extends Controller
|
|||||||
/* add column Actions */
|
/* add column Actions */
|
||||||
/* there are action buttons and modal popup to delete a data column */
|
/* there are action buttons and modal popup to delete a data column */
|
||||||
->addColumn('Actions', function ($model) {
|
->addColumn('Actions', function ($model) {
|
||||||
return '<span data-toggle="modal" data-target="#deletepage'.$model->id.'"><a href="#" ><button class="btn btn-danger btn-xs"></a> '.\Lang::get('lang.delete').'</button></span> <a href=page/'.$model->slug.'/edit class="btn btn-warning btn-xs">'.\Lang::get('lang.edit').'</a> <a href=pages/'.$model->slug.' class="btn btn-primary btn-xs">'.\Lang::get('lang.view').'</a>
|
return '<span data-toggle="modal" data-target="#deletepage'.$model->id.'"><a href="#" ><button class="btn btn-danger btn-xs"></a> '.\Lang::get('lang.delete').'</button></span> <a href=page/'.$model->id.'/edit class="btn btn-warning btn-xs">'.\Lang::get('lang.edit').'</a> <a href=pages/'.$model->slug.' class="btn btn-primary btn-xs">'.\Lang::get('lang.view').'</a>
|
||||||
<div class="modal fade" id="deletepage'.$model->id.'">
|
<div class="modal fade" id="deletepage'.$model->id.'">
|
||||||
<div class="modal-dialog">
|
<div class="modal-dialog">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
@@ -150,7 +150,7 @@ class PageController extends Controller
|
|||||||
public function edit($slug)
|
public function edit($slug)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$page = $this->page->where('slug', $slug)->first();
|
$page = $this->page->where('id', $slug)->first();
|
||||||
|
|
||||||
return view('themes.default1.agent.kb.pages.edit', compact('page'));
|
return view('themes.default1.agent.kb.pages.edit', compact('page'));
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@@ -169,7 +169,7 @@ class PageController extends Controller
|
|||||||
public function update($slug, PageRequest $request)
|
public function update($slug, PageRequest $request)
|
||||||
{
|
{
|
||||||
// get pages with respect to slug
|
// get pages with respect to slug
|
||||||
$pages = $this->page->where('slug', $slug)->first();
|
$pages = $this->page->where('id', $slug)->first();
|
||||||
$sl = $request->input('name');
|
$sl = $request->input('name');
|
||||||
$slug = Str::slug($sl, '-');
|
$slug = Str::slug($sl, '-');
|
||||||
|
|
||||||
|
@@ -42,7 +42,7 @@ class SettingsController extends Controller
|
|||||||
// checking authentication
|
// checking authentication
|
||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
// checking roles
|
// checking roles
|
||||||
$this->middleware('roles');
|
$this->middleware('role.agent');
|
||||||
$this->language();
|
$this->language();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -124,7 +124,7 @@ class UserController extends Controller
|
|||||||
if ($arti) {
|
if ($arti) {
|
||||||
return view('themes.default1.client.kb.article-list.show', compact('arti'));
|
return view('themes.default1.client.kb.article-list.show', compact('arti'));
|
||||||
} else {
|
} else {
|
||||||
return redirect('404');
|
return Redirect::back()->with('fails', Lang::get('lang.sorry_not_processed'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +150,6 @@ class UserController extends Controller
|
|||||||
if (Config::get('database.install') == '%0%') {
|
if (Config::get('database.install') == '%0%') {
|
||||||
return redirect('step1');
|
return redirect('step1');
|
||||||
} else {
|
} else {
|
||||||
//$categorys = $category->get();
|
|
||||||
$categorys = $category->get();
|
$categorys = $category->get();
|
||||||
// $categorys->setPath('home');
|
// $categorys->setPath('home');
|
||||||
/* direct to view with $article_id */
|
/* direct to view with $article_id */
|
||||||
@@ -235,7 +234,6 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'comment' => 'required',
|
'comment' => 'required',
|
||||||
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$article = $article->where('slug', $slug)->first();
|
$article = $article->where('slug', $slug)->first();
|
||||||
@@ -265,7 +263,16 @@ class UserController extends Controller
|
|||||||
|
|
||||||
public function getPage($name, Page $page)
|
public function getPage($name, Page $page)
|
||||||
{
|
{
|
||||||
$page = $page->where('slug', $name)->first();
|
$page = $page->where('slug', $name);
|
||||||
|
|
||||||
|
if (!Auth::check() || \Auth::user()->role == 'user') {
|
||||||
|
$page = $page
|
||||||
|
->where(['status' => 1, 'visibility'=>1])
|
||||||
|
->first();
|
||||||
|
} else {
|
||||||
|
$page = $page->where('status', 1)->first();
|
||||||
|
}
|
||||||
|
|
||||||
if ($page) {
|
if ($page) {
|
||||||
return view('themes.default1.client.kb.article-list.pages', compact('page'));
|
return view('themes.default1.client.kb.article-list.pages', compact('page'));
|
||||||
} else {
|
} else {
|
||||||
|
@@ -24,8 +24,7 @@ class ArticleRequest extends Request
|
|||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'required',
|
'name' => 'required|unique:kb_article',
|
||||||
//'slug' => 'required|unique:kb_article',
|
|
||||||
'description' => 'required',
|
'description' => 'required',
|
||||||
'category_id' => 'required',
|
'category_id' => 'required',
|
||||||
];
|
];
|
||||||
|
@@ -23,11 +23,14 @@ class ArticleUpdate extends Request
|
|||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
$id = $this->segments()[1];
|
//$id = $this->segments()[1];
|
||||||
|
$segments = $this->segments();
|
||||||
|
|
||||||
|
$id = isset($segments[1]) ? $segments[1] : null;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => 'required',
|
'name' => 'required|unique:kb_article,name,'.$id,
|
||||||
'slug' => 'required|unique:kb_article,slug,'.$id.',id',
|
'slug' => 'required|unique:kb_article,slug,'.$id,
|
||||||
'description' => 'required',
|
'description' => 'required',
|
||||||
'category_id' => 'required',
|
'category_id' => 'required',
|
||||||
];
|
];
|
||||||
|
@@ -23,10 +23,11 @@ class PageRequest extends Request
|
|||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
$slug = $this->segment(2);
|
$id = $this->segment(2);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => 'required|unique:kb_pages,slug,'.$slug,
|
'name' => 'required|unique:kb_pages,name,'.$id,
|
||||||
|
'description' => 'required',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
phpunit.xml
33
phpunit.xml
@@ -1,27 +1,36 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="bootstrap/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
<phpunit backupGlobals="false"
|
||||||
<coverage includeUncoveredFiles="true">
|
backupStaticAttributes="false"
|
||||||
<include>
|
bootstrap="bootstrap/autoload.php"
|
||||||
<directory suffix=".php">app/</directory>
|
colors="true"
|
||||||
</include>
|
convertErrorsToExceptions="true"
|
||||||
</coverage>
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false">
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="feature">
|
<testsuite name="all">
|
||||||
<directory>tests/Feature</directory>
|
<directory>./tests/</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
<testsuite name="unit">
|
<testsuite name="unit">
|
||||||
<directory>tests/Unit</directory>
|
<directory>./tests/unit</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
<filter>
|
||||||
|
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||||
|
<directory suffix=".php">app/</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
<php>
|
<php>
|
||||||
<env name="APP_ENV" value="testing"/>
|
<env name="APP_ENV" value="testing"/>
|
||||||
<!-- <env name="DB_CONNECTION" value="testing"/>
|
<env name="DB_CONNECTION" value="testing"/>
|
||||||
<env name="DB_DATABASE" value="testing_db"/> -->
|
<env name="DB_DATABASE" value="testing_db"/>
|
||||||
<env name="CACHE_DRIVER" value="array"/>
|
<env name="CACHE_DRIVER" value="array"/>
|
||||||
<env name="SESSION_DRIVER" value="array"/>
|
<env name="SESSION_DRIVER" value="array"/>
|
||||||
<env name="QUEUE_CONNECTION" value="sync"/>
|
<env name="QUEUE_CONNECTION" value="sync"/>
|
||||||
<env name="MAIL_MAILER" value="mail"/>
|
<env name="MAIL_MAILER" value="mail"/>
|
||||||
<env name="APP_URL" value="http://localhost:8000"/>
|
<env name="APP_URL" value="http://localhost:8000"/>
|
||||||
|
<env name="DB_DATABASE" value="testing_db"/>
|
||||||
<env name="DB_INSTALL" value="1"/>
|
<env name="DB_INSTALL" value="1"/>
|
||||||
</php>
|
</php>
|
||||||
</phpunit>
|
</phpunit>
|
@@ -83,7 +83,7 @@ class="nav-item menu-open"
|
|||||||
|
|
||||||
<div class="form-group col-sm-12 {{ $errors->has('description') ? 'has-error' : '' }}">
|
<div class="form-group col-sm-12 {{ $errors->has('description') ? 'has-error' : '' }}">
|
||||||
{!! Form::label('description',Lang::get('lang.description')) !!}
|
{!! Form::label('description',Lang::get('lang.description')) !!}
|
||||||
|
<span class="text-red"> *</span>
|
||||||
<div class="form-group" style="background-color:white">
|
<div class="form-group" style="background-color:white">
|
||||||
{!! Form::textarea('description',null,['class' => 'form-control color','size' => '110x15','id'=>'myNicEditor','placeholder'=>Lang::get('lang.enter_the_description')]) !!}
|
{!! Form::textarea('description',null,['class' => 'form-control color','size' => '110x15','id'=>'myNicEditor','placeholder'=>Lang::get('lang.enter_the_description')]) !!}
|
||||||
</div>
|
</div>
|
||||||
|
@@ -35,7 +35,7 @@ class="nav-item menu-open"
|
|||||||
|
|
||||||
@section('content')
|
@section('content')
|
||||||
|
|
||||||
{!! Form::model($page,['url' => 'page/'.$page->slug, 'method' => 'PATCH','files'=>true]) !!}
|
{!! Form::model($page,['url' => 'page/'.$page->id, 'method' => 'PATCH','files'=>true]) !!}
|
||||||
|
|
||||||
@if(Session::has('errors'))
|
@if(Session::has('errors'))
|
||||||
<?php //dd($errors); ?>
|
<?php //dd($errors); ?>
|
||||||
@@ -130,7 +130,7 @@ class="nav-item menu-open"
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-footer">
|
<div class="card-footer">
|
||||||
|
|
||||||
{!! Form::submit(Lang::get('lang.publish'),['class'=>'btn btn-primary'])!!}
|
{!! Form::submit(Lang::get('lang.publish'),['class'=>'btn btn-primary'])!!}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -254,9 +254,22 @@ $category_id = $all->pluck('category_id')->toArray();
|
|||||||
$(".form-group").removeClass("has-error");
|
$(".form-group").removeClass("has-error");
|
||||||
|
|
||||||
// Perform your custom validation here
|
// Perform your custom validation here
|
||||||
var name = $("#comment-name").val().trim();
|
var nameElement = $("#comment-name");
|
||||||
var email = $("#comment-email").val().trim();
|
var emailElement = $("#comment-email");
|
||||||
var comment = $("#comment-comment").val().trim();
|
var commentElement = $("#comment-comment");
|
||||||
|
|
||||||
|
if (nameElement.length > 0) {
|
||||||
|
var name = nameElement.val().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (emailElement.length > 0) {
|
||||||
|
var email = emailElement.val().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commentElement.length > 0) {
|
||||||
|
var comment = commentElement.val().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Flag to track if there are any errors
|
// Flag to track if there are any errors
|
||||||
var hasErrors = false;
|
var hasErrors = false;
|
||||||
|
@@ -160,7 +160,11 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<?php $pages = App\Model\kb\Page::where('status', '1')->where('visibility', '1')->get();
|
<?php
|
||||||
|
if(!Auth::check() || Auth::user()->role == 'user')
|
||||||
|
$pages = App\Model\kb\Page::where('status', '1')->where('visibility', '1')->get();
|
||||||
|
else
|
||||||
|
$pages = App\Model\kb\Page::where('status', '1')->get();
|
||||||
?>
|
?>
|
||||||
@if(count($pages))
|
@if(count($pages))
|
||||||
<li @yield('pages') class="nav-item dropdown">
|
<li @yield('pages') class="nav-item dropdown">
|
||||||
|
243
tests/Unit/ArticleControllerTest.php
Normal file
243
tests/Unit/ArticleControllerTest.php
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Http\Requests\kb\ArticleRequest;
|
||||||
|
use App\Http\Requests\kb\ArticleUpdate;
|
||||||
|
use App\Http\Requests\kb\CategoryRequest;
|
||||||
|
use App\Model\kb\Article;
|
||||||
|
use App\Model\kb\Category;
|
||||||
|
use App\Model\kb\Relationship;
|
||||||
|
use App\User;
|
||||||
|
use Faker\Factory as FakerFactory;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Lang;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ArticleControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
//use DatabaseTransactions;
|
||||||
|
protected $user; // Declare a user property
|
||||||
|
|
||||||
|
// Set up the authenticated user before each test
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$faker = FakerFactory::create();
|
||||||
|
|
||||||
|
//Create User -> Agent
|
||||||
|
|
||||||
|
//$str = Str::random(10);
|
||||||
|
$str = 'demopass';
|
||||||
|
$password = Hash::make($str);
|
||||||
|
$email = $faker->unique()->email();
|
||||||
|
$user = new User([
|
||||||
|
'first_name' => $faker->firstName(),
|
||||||
|
'last_name' => $faker->lastName(),
|
||||||
|
'email' => $email,
|
||||||
|
'user_name' => $faker->unique()->userName(),
|
||||||
|
'password' => $password,
|
||||||
|
'assign_group' => 1,
|
||||||
|
'primary_dpt' => 1,
|
||||||
|
'active' => 1,
|
||||||
|
'role' => 'agent',
|
||||||
|
'agent_tzone' => 81,
|
||||||
|
]);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
// Check if data is inserted
|
||||||
|
$this->assertDatabaseHas('users', ['email'=>$email]);
|
||||||
|
|
||||||
|
// Authenticate as the created user
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$this->assertAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_display_the_article_index_page()
|
||||||
|
{
|
||||||
|
$response = $this->get(route('article.index'));
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStoreArticleWithCategories()
|
||||||
|
{
|
||||||
|
// Create a Category model for testing
|
||||||
|
$data = [
|
||||||
|
'name' => 'Test Category',
|
||||||
|
'description' => 'Test Category Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
|
||||||
|
$response = $this->post(route('category.store'), $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$response->assertSessionHas('success');
|
||||||
|
$this->assertDatabaseHas('kb_category', $data);
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
// Article data
|
||||||
|
$articleData = [
|
||||||
|
'name' => 'Test Article',
|
||||||
|
'description'=> 'Test Article Description',
|
||||||
|
'category_id'=> $category->id,
|
||||||
|
'year' => '2023',
|
||||||
|
'month' => '10',
|
||||||
|
'day' => '03',
|
||||||
|
'hour' => '12',
|
||||||
|
'minute' => '30',
|
||||||
|
];
|
||||||
|
|
||||||
|
$articleRequest = new ArticleRequest($articleData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
try {
|
||||||
|
$validator = Validator::make($articleData, (new ArticleRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
$response = $this->post(route('article.store'), $articleData);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$response = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
if ($response) {
|
||||||
|
$response->assertStatus(200); // Check if the response status code is a redirect (302)
|
||||||
|
|
||||||
|
$article = Article::latest()->first();
|
||||||
|
|
||||||
|
$article_relationship = new Relationship();
|
||||||
|
|
||||||
|
$article_relationship->category_id = $category->id;
|
||||||
|
$article_relationship->article_id = $article->id;
|
||||||
|
$article_relationship->save();
|
||||||
|
|
||||||
|
// Verify that the article was created and the category relationship exists
|
||||||
|
$this->assertDatabaseHas('kb_article', [
|
||||||
|
'name' => $articleData['name'],
|
||||||
|
'slug' => Str::slug($articleData['name'], '-'),
|
||||||
|
'publish_time' => $articleData['year'].'-'.$articleData['month'].'-'.$articleData['day'].' '.$articleData['hour'].':'.$articleData['minute'].':00',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Check if the category relationship exists
|
||||||
|
$this->assertDatabaseHas('kb_article_relationship', [
|
||||||
|
'category_id' => $category->id,
|
||||||
|
'article_id' => Article::latest()->first()->id, // Get the ID of the latest created article
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
$this->fail('Exception thrown: '.$e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEditArticle()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$article = Article::latest()->first(); // Create a sample Article for testing
|
||||||
|
$relationship = Relationship::latest()->first(); // Create a sample Relationship for testing
|
||||||
|
$category = Category::latest()->first(); // Create a sample Category for testing
|
||||||
|
|
||||||
|
$assign = $relationship->where('article_id', 'id')->pluck('category_id');
|
||||||
|
$category = $category->pluck('id', 'name');
|
||||||
|
|
||||||
|
$response = $this->get(
|
||||||
|
"/article/{$article->id}/edit",
|
||||||
|
['category' => $category,
|
||||||
|
'article' => $article,
|
||||||
|
'assign' => $assign,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateArticle()
|
||||||
|
{
|
||||||
|
$article = Article::latest()->first();
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'id' => $article->id,
|
||||||
|
'name' => 'Updated Article Name',
|
||||||
|
'description' => 'Updated Description',
|
||||||
|
'slug' => Str::slug('Updated Article Name', '-'),
|
||||||
|
'category_id' => [1, 2],
|
||||||
|
'year' => '2023',
|
||||||
|
'month' => '10',
|
||||||
|
'day' => '03',
|
||||||
|
'hour' => '2',
|
||||||
|
'minute' => '20',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new ArticleUpdate())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
|
||||||
|
$response = $this->put(route('article.update', $article->id), $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302);
|
||||||
|
|
||||||
|
$article_relationship = Relationship::latest()->first();
|
||||||
|
$article_relationship = $article_relationship->where('article_id', $article->id);
|
||||||
|
$article_relationship->delete();
|
||||||
|
|
||||||
|
$article = Article::latest()->first();
|
||||||
|
$relation = new Relationship();
|
||||||
|
$relation->category_id = $category->id;
|
||||||
|
$relation->article_id = $article->id;
|
||||||
|
$relation->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_delete_a_category()
|
||||||
|
{
|
||||||
|
// Create a sample article, relationship
|
||||||
|
$article = Article::latest()->first();
|
||||||
|
$relationship = Relationship::find($article->id);
|
||||||
|
|
||||||
|
// Ensure the destroy route works as expected
|
||||||
|
|
||||||
|
$response = $this->get("/article/delete/{$article->slug}");
|
||||||
|
|
||||||
|
// Assert that success message is flashed
|
||||||
|
$response->assertSessionHas('success', Lang::get('lang.article_deleted_successfully'));
|
||||||
|
|
||||||
|
// Create a category
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
// Create a related relationship (you may need to adjust this based on your actual relationships)
|
||||||
|
$relation = Relationship::find($category->id);
|
||||||
|
|
||||||
|
// Call the destroy method with the category ID
|
||||||
|
$response = $this->get("/category/delete/{$category->id}");
|
||||||
|
|
||||||
|
// Assert that the category is deleted from the database
|
||||||
|
$this->assertDatabaseMissing('kb_category', ['id' => $category->id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function it_cannot_delete_a_article_if_related()
|
||||||
|
{
|
||||||
|
// Create a category
|
||||||
|
$article = Article::find(1);
|
||||||
|
|
||||||
|
// Call the destroy method with the category ID (without creating related records)
|
||||||
|
$response = $this->get("/article/delete/{$article->slug}");
|
||||||
|
|
||||||
|
// Assert that the category is not deleted from the database
|
||||||
|
$this->assertDatabaseHas('kb_article', ['id' => $article->id]);
|
||||||
|
|
||||||
|
// Assert that the response is a redirect
|
||||||
|
$response->assertRedirect();
|
||||||
|
|
||||||
|
// Assert that the response has a failure message
|
||||||
|
$response->assertSessionHas('fails', Lang::get('lang.article_not_deleted'));
|
||||||
|
}
|
||||||
|
}
|
233
tests/Unit/CategoryControllerTest.php
Normal file
233
tests/Unit/CategoryControllerTest.php
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Http\Requests\kb\CategoryRequest;
|
||||||
|
use App\Model\kb\Category;
|
||||||
|
use App\Model\kb\Relationship;
|
||||||
|
use App\User;
|
||||||
|
use Faker\Factory as FakerFactory;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Lang;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CategoryControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
//use DatabaseTransactions;
|
||||||
|
protected $user; // Declare a user property
|
||||||
|
|
||||||
|
// Set up the authenticated user before each test
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$faker = FakerFactory::create();
|
||||||
|
|
||||||
|
//Create User -> Agent
|
||||||
|
|
||||||
|
//$str = Str::random(10);
|
||||||
|
$str = 'demopass';
|
||||||
|
$password = Hash::make($str);
|
||||||
|
$email = $faker->unique()->email();
|
||||||
|
$user = new User([
|
||||||
|
'first_name' => $faker->firstName(),
|
||||||
|
'last_name' => $faker->lastName(),
|
||||||
|
'email' => $email,
|
||||||
|
'user_name' => $faker->unique()->userName(),
|
||||||
|
'password' => $password,
|
||||||
|
'assign_group' => 1,
|
||||||
|
'primary_dpt' => 1,
|
||||||
|
'active' => 1,
|
||||||
|
'role' => 'agent',
|
||||||
|
'agent_tzone' => 81,
|
||||||
|
]);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
// Check if data is inserted
|
||||||
|
$this->assertDatabaseHas('users', ['email'=>$email]);
|
||||||
|
|
||||||
|
// Authenticate as the created user
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$this->assertAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_display_the_category_index_page()
|
||||||
|
{
|
||||||
|
$response = $this->get(route('category.index'));
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationPasses()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'New Category',
|
||||||
|
'description' => 'Category Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
|
||||||
|
$response = $this->post(route('category.store'), $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$response->assertSessionHas('success');
|
||||||
|
$this->assertDatabaseHas('kb_category', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenNameMissing()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'description' => 'Category Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenNameExceedsMaxLength()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => str_repeat('A', 251),
|
||||||
|
'description' => 'Category Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenNameNotUnique()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'Greetings',
|
||||||
|
'description' => 'Category Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenDescriptionMissing()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'New Category',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('description'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEditCategory()
|
||||||
|
{
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
$categories = Category::pluck('name', 'id')->toArray();
|
||||||
|
$response = $this->get(
|
||||||
|
"/category/{$category->id}/edit",
|
||||||
|
['category' => $category,
|
||||||
|
'categories' => $categories,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_update_an_existing_category()
|
||||||
|
{
|
||||||
|
// Retrieve an existing category from the database
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'Updated Category Name',
|
||||||
|
'description' => 'Updated Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
|
||||||
|
$response = $this->put(route('category.update', $category->id), $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$response->assertSessionHas('success');
|
||||||
|
$this->assertDatabaseHas('kb_category', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_cannot_update_an_existing_category()
|
||||||
|
{
|
||||||
|
// Retrieve an existing category from the database
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'Greetings',
|
||||||
|
'description' => 'Updated Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new CategoryRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$response = $this->put(route('category.update', $category->id), $data);
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_delete_a_category()
|
||||||
|
{
|
||||||
|
// Create a category
|
||||||
|
$category = Category::latest()->first();
|
||||||
|
|
||||||
|
// Create a related relationship (you may need to adjust this based on your actual relationships)
|
||||||
|
$relation = Relationship::find($category->id);
|
||||||
|
|
||||||
|
// Call the destroy method with the category ID
|
||||||
|
$response = $this->get("/category/delete/{$category->id}");
|
||||||
|
|
||||||
|
// Assert that the category is deleted from the database
|
||||||
|
$this->assertDatabaseMissing('kb_category', ['id' => $category->id]);
|
||||||
|
|
||||||
|
// Assert that the response is a redirect
|
||||||
|
$response->assertRedirect();
|
||||||
|
|
||||||
|
// Assert that the response has a success message
|
||||||
|
$response->assertSessionHas('success', Lang::get('lang.category_deleted_successfully'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_cannot_delete_a_category_if_related()
|
||||||
|
{
|
||||||
|
// Create a category
|
||||||
|
$category = Category::find(1);
|
||||||
|
|
||||||
|
// Call the destroy method with the category ID (without creating related records)
|
||||||
|
$response = $this->get("/category/delete/{$category->id}");
|
||||||
|
|
||||||
|
// Assert that the category is not deleted from the database
|
||||||
|
$this->assertDatabaseHas('kb_category', ['id' => $category->id]);
|
||||||
|
|
||||||
|
// Assert that the response is a redirect
|
||||||
|
$response->assertRedirect();
|
||||||
|
|
||||||
|
// Assert that the response has a failure message
|
||||||
|
$response->assertSessionHas('fails', Lang::get('lang.category_not_deleted'));
|
||||||
|
}
|
||||||
|
}
|
184
tests/Unit/PageControllerTest.php
Normal file
184
tests/Unit/PageControllerTest.php
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Http\Requests\kb\PageRequest;
|
||||||
|
use App\Model\kb\Page;
|
||||||
|
use App\User;
|
||||||
|
use Faker\Factory as FakerFactory;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class PageControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions;
|
||||||
|
protected $user; // Declare a user property
|
||||||
|
|
||||||
|
// Set up the authenticated user before each test
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$faker = FakerFactory::create();
|
||||||
|
|
||||||
|
//Create User -> Agent
|
||||||
|
|
||||||
|
//$str = Str::random(10);
|
||||||
|
$str = 'demopass';
|
||||||
|
$password = Hash::make($str);
|
||||||
|
$email = $faker->unique()->email();
|
||||||
|
$user = new User([
|
||||||
|
'first_name' => $faker->firstName(),
|
||||||
|
'last_name' => $faker->lastName(),
|
||||||
|
'email' => $email,
|
||||||
|
'user_name' => $faker->unique()->userName(),
|
||||||
|
'password' => $password,
|
||||||
|
'assign_group' => 1,
|
||||||
|
'primary_dpt' => 1,
|
||||||
|
'active' => 1,
|
||||||
|
'role' => 'agent',
|
||||||
|
'agent_tzone' => 81,
|
||||||
|
]);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
// Check if data is inserted
|
||||||
|
$this->assertDatabaseHas('users', ['email'=>$email]);
|
||||||
|
|
||||||
|
// Authenticate as the created user
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$this->assertAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_display_the_page_index_page()
|
||||||
|
{
|
||||||
|
$response = $this->get(route('page.index'));
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateMethod()
|
||||||
|
{
|
||||||
|
$response = $this->get('/page/create');
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationPasses()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'New Page',
|
||||||
|
'description' => 'Page Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
|
||||||
|
$response = $this->post(route('page.store'), $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$response->assertSessionHas('success');
|
||||||
|
$this->assertDatabaseHas('kb_pages', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenNameMissing()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'description' => 'Page Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenNameNotUnique()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'Page1',
|
||||||
|
'description' => 'Page Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidationFailsWhenDescriptionMissing()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'name' => 'New',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('description'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEditPage()
|
||||||
|
{
|
||||||
|
$page = Page::latest()->first();
|
||||||
|
|
||||||
|
$response = $this->get('/page/'.$page->id.'/edit');
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdatePage()
|
||||||
|
{
|
||||||
|
$page = Page::latest()->first();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'Updated Page Name',
|
||||||
|
'description' => 'Updated Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertTrue($validator->passes());
|
||||||
|
$response = $this->put('/page/'.$page->id, $data);
|
||||||
|
|
||||||
|
$response->assertStatus(302); // Assuming a successful update redirects
|
||||||
|
$this->assertDatabaseHas('kb_pages', $data);
|
||||||
|
// You can add more assertions as needed.
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCannotUpdatePage()
|
||||||
|
{
|
||||||
|
$page = Page::latest()->first();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'Page1',
|
||||||
|
'description' => 'Updated Description',
|
||||||
|
];
|
||||||
|
|
||||||
|
$validator = Validator::make($data, (new PageRequest())->rules());
|
||||||
|
|
||||||
|
$this->assertFalse($validator->passes());
|
||||||
|
$response = $this->put('/page/'.$page->id, $data);
|
||||||
|
$response->assertStatus(302);
|
||||||
|
$this->assertTrue($validator->fails());
|
||||||
|
$this->assertTrue($validator->errors()->has('name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDestroyMethod()
|
||||||
|
{
|
||||||
|
$page = Page::latest()->first();
|
||||||
|
|
||||||
|
$response = $this->delete('/page/'.$page->id);
|
||||||
|
|
||||||
|
$response->assertStatus(302); // Assuming a successful deletion redirects
|
||||||
|
$this->assertDatabaseMissing('kb_pages', ['id' => $page->id]);
|
||||||
|
// You can add more assertions as needed.
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user