Apply fixes from StyleCI

This commit is contained in:
StyleCI Bot
2023-10-26 12:55:28 +00:00
committed by RafficMohammed
parent ccf417998c
commit 36396e6460
4 changed files with 181 additions and 216 deletions

View File

@@ -10,7 +10,6 @@ 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;
@@ -19,7 +18,6 @@ use Tests\TestCase;
class ArticleControllerTest extends TestCase
{
protected $user; // Declare a user property
// Set up the authenticated user before each test
@@ -36,16 +34,16 @@ class ArticleControllerTest extends TestCase
$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,
'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,
'primary_dpt' => 1,
'active' => 1,
'role' => 'agent',
'agent_tzone' => 81,
]);
$user->save();
@@ -56,201 +54,188 @@ class ArticleControllerTest extends TestCase
$this->actingAs($user);
$this->assertAuthenticated();
}
/** @test */
public
function it_can_display_the_article_index_page()
{
$response = $this->get(route('article.index'));
/** @test */
public function it_can_display_the_article_index_page()
{
$response = $this->get(route('article.index'));
$response->assertStatus(200);
}
$response->assertStatus(200);
}
public
function testStoreArticleWithCategories()
{
public function testStoreArticleWithCategories()
{
// Create a Category model for testing
$data = [
'name' => 'Test Category',
'description' => 'Test Category Description',
];
// Create a Category model for testing
$data = [
'name' => 'Test Category',
'description' => 'Test Category Description',
];
$validator = Validator::make($data, (new CategoryRequest())->rules());
$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;
}
$response = $this->post(route('category.store'), $data);
// Assert
if ($response) {
$response->assertStatus(200); // Check if the response status code is a redirect (302)
$response->assertStatus(302);
$response->assertSessionHas('success');
$this->assertDatabaseHas('kb_category', $data);
$category = Category::latest()->first();
$article = Article::latest()->first();
// Article data
$articleData = [
'name' => 'Test Article',
'description' => 'Test Article Description',
$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,
'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'));
'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'));
}
}

View File

@@ -7,7 +7,6 @@ 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;
@@ -57,7 +56,6 @@ class CategoryControllerTest extends TestCase
/** @test */
public function it_can_display_the_category_index_page()
{
$response = $this->get(route('category.index'));
$response->assertStatus(200);
@@ -65,7 +63,6 @@ class CategoryControllerTest extends TestCase
public function testValidationPasses()
{
$data = [
'name' => 'New Category',
'description' => 'Category Description',
@@ -83,7 +80,6 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameMissing()
{
$data = [
'description' => 'Category Description',
];
@@ -97,7 +93,6 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameExceedsMaxLength()
{
$data = [
'name' => str_repeat('A', 251),
'description' => 'Category Description',
@@ -112,7 +107,6 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameNotUnique()
{
$data = [
'name' => 'New Category',
'description' => 'Category Description',
@@ -127,7 +121,6 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenDescriptionMissing()
{
$data = [
'name' => 'New Category',
];
@@ -141,7 +134,6 @@ class CategoryControllerTest extends TestCase
public function testEditCategory()
{
$category = Category::latest()->first();
$categories = Category::pluck('name', 'id')->toArray();
$response = $this->get(
@@ -156,7 +148,6 @@ class CategoryControllerTest extends TestCase
/** @test */
public function it_can_update_an_existing_category()
{
// Retrieve an existing category from the database
$category = Category::latest()->first();
@@ -179,7 +170,6 @@ class CategoryControllerTest extends TestCase
/** @test */
public function it_cannot_update_an_existing_category()
{
// Retrieve an existing category from the database
$category = Category::latest()->first();
@@ -200,7 +190,6 @@ class CategoryControllerTest extends TestCase
/** @test */
public function it_can_delete_a_category()
{
// Create a category
$category = Category::latest()->first();
@@ -217,7 +206,6 @@ class CategoryControllerTest extends TestCase
$response->assertRedirect();
// Assert that the response has a success message
$response->assertSessionHas('success', Lang::get('lang.category_deleted_successfully'));
}
$response->assertSessionHas('success', Lang::get('lang.category_deleted_successfully'));
}
}

View File

@@ -6,8 +6,6 @@ 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\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

View File

@@ -5,14 +5,11 @@ namespace Tests\Unit;
use App\Model\helpdesk\Ticket\Ticket_Thread;
use App\Model\helpdesk\Ticket\Tickets;
use App\User;
use DateTimeZone;
use Faker\Factory as FakerFactory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Tests\TestCase;
use UTC;
class TicketControllerTest extends TestCase
{
@@ -102,7 +99,6 @@ class TicketControllerTest extends TestCase
// Assert that the response status is 200 (OK).
$response->assertStatus(200);
}
//Testing Reply Alert and Last Activity filed
@@ -144,7 +140,5 @@ class TicketControllerTest extends TestCase
$response3 = $this->post(route('ticket.reply', ['id' => $tickets->id]), $replyData);
$response3->assertStatus(200);
$response3->assertSee(Lang::get('lang.you_have_successfully_replied_to_your_ticket'));
}
}