test_case_fixes

This commit is contained in:
KNaveenraj-ladybird
2023-10-26 18:24:25 +05:30
parent a184f0b7df
commit 17f3d11c32
5 changed files with 247 additions and 268 deletions

View File

@@ -8,7 +8,6 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Database\Seeders\v_2_0_0\DatabaseSeeder;
class SetupTestEnv extends Command class SetupTestEnv extends Command
{ {

View File

@@ -19,7 +19,7 @@ use Tests\TestCase;
class ArticleControllerTest extends TestCase class ArticleControllerTest extends TestCase
{ {
//use DatabaseTransactions;
protected $user; // Declare a user property protected $user; // Declare a user property
// Set up the authenticated user before each test // Set up the authenticated user before each test
@@ -36,208 +36,221 @@ class ArticleControllerTest extends TestCase
$password = Hash::make($str); $password = Hash::make($str);
$email = $faker->unique()->email(); $email = $faker->unique()->email();
$user = new User([ $user = new User([
'first_name' => $faker->firstName(), 'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(), 'last_name' => $faker->lastName(),
'email' => $email, 'email' => $email,
'user_name' => $faker->unique()->userName(), 'user_name' => $faker->unique()->userName(),
'password' => $password, 'password' => $password,
'assign_group' => 1, 'assign_group' => 1,
'primary_dpt' => 1, 'primary_dpt' => 1,
'active' => 1, 'active' => 1,
'role' => 'agent', 'role' => 'agent',
'agent_tzone' => 81, 'agent_tzone' => 81,
]); ]);
$user->save(); $user->save();
// Check if data is inserted // Check if data is inserted
$this->assertDatabaseHas('users', ['email'=>$email]); $this->assertDatabaseHas('users', ['email' => $email]);
// Authenticate as the created user // Authenticate as the created user
$this->actingAs($user); $this->actingAs($user);
$this->assertAuthenticated(); $this->assertAuthenticated();
} }
/** @test */
public
function it_can_display_the_article_index_page()
{
/** @test */ $response = $this->get(route('article.index'));
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',
];
$validator = Validator::make($data, (new CategoryRequest())->rules()); // Create a Category model for testing
$data = [
'name' => 'Test Category',
'description' => 'Test Category Description',
];
$this->assertTrue($validator->passes()); $validator = Validator::make($data, (new CategoryRequest())->rules());
$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()); $this->assertTrue($validator->passes());
$response = $this->post(route('article.store'), $articleData);
$response->assertStatus(200); $response = $this->post(route('category.store'), $data);
} catch (Exception $e) {
$response = null; $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());
}
} }
// Assert public
if ($response) { function testEditArticle()
$response->assertStatus(200); // Check if the response status code is a redirect (302) {
// 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(); $article = Article::latest()->first();
$category = Category::latest()->first();
$article_relationship = new Relationship(); $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',
];
$article_relationship->category_id = $category->id; $validator = Validator::make($data, (new ArticleUpdate())->rules());
$article_relationship->article_id = $article->id;
$article_relationship->save();
// Verify that the article was created and the category relationship exists $this->assertTrue($validator->passes());
$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 $response = $this->put(route('article.update', $article->id, $data));
$this->assertDatabaseHas('kb_article_relationship', [
'category_id' => $category->id, $response->assertStatus(302);
'article_id' => Article::latest()->first()->id, // Get the ID of the latest created article
]); $article_relationship = Relationship::latest()->first();
} else { $article_relationship = $article_relationship->where('article_id', $article->id);
$this->fail('Exception thrown: '.$e->getMessage()); $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'));
} }
} }
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

@@ -16,7 +16,6 @@ use Tests\TestCase;
class CategoryControllerTest extends TestCase class CategoryControllerTest extends TestCase
{ {
//use DatabaseTransactions;
protected $user; // Declare a user property protected $user; // Declare a user property
// Set up the authenticated user before each test // Set up the authenticated user before each test
@@ -58,6 +57,7 @@ class CategoryControllerTest extends TestCase
/** @test */ /** @test */
public function it_can_display_the_category_index_page() public function it_can_display_the_category_index_page()
{ {
$response = $this->get(route('category.index')); $response = $this->get(route('category.index'));
$response->assertStatus(200); $response->assertStatus(200);
@@ -65,6 +65,7 @@ class CategoryControllerTest extends TestCase
public function testValidationPasses() public function testValidationPasses()
{ {
$data = [ $data = [
'name' => 'New Category', 'name' => 'New Category',
'description' => 'Category Description', 'description' => 'Category Description',
@@ -75,7 +76,6 @@ class CategoryControllerTest extends TestCase
$this->assertTrue($validator->passes()); $this->assertTrue($validator->passes());
$response = $this->post(route('category.store'), $data); $response = $this->post(route('category.store'), $data);
$response->assertStatus(302); $response->assertStatus(302);
$response->assertSessionHas('success'); $response->assertSessionHas('success');
$this->assertDatabaseHas('kb_category', $data); $this->assertDatabaseHas('kb_category', $data);
@@ -83,6 +83,7 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameMissing() public function testValidationFailsWhenNameMissing()
{ {
$data = [ $data = [
'description' => 'Category Description', 'description' => 'Category Description',
]; ];
@@ -96,6 +97,7 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameExceedsMaxLength() public function testValidationFailsWhenNameExceedsMaxLength()
{ {
$data = [ $data = [
'name' => str_repeat('A', 251), 'name' => str_repeat('A', 251),
'description' => 'Category Description', 'description' => 'Category Description',
@@ -110,8 +112,9 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenNameNotUnique() public function testValidationFailsWhenNameNotUnique()
{ {
$data = [ $data = [
'name' => 'Greetings', 'name' => 'New Category',
'description' => 'Category Description', 'description' => 'Category Description',
]; ];
@@ -124,6 +127,7 @@ class CategoryControllerTest extends TestCase
public function testValidationFailsWhenDescriptionMissing() public function testValidationFailsWhenDescriptionMissing()
{ {
$data = [ $data = [
'name' => 'New Category', 'name' => 'New Category',
]; ];
@@ -137,6 +141,7 @@ class CategoryControllerTest extends TestCase
public function testEditCategory() public function testEditCategory()
{ {
$category = Category::latest()->first(); $category = Category::latest()->first();
$categories = Category::pluck('name', 'id')->toArray(); $categories = Category::pluck('name', 'id')->toArray();
$response = $this->get( $response = $this->get(
@@ -151,6 +156,7 @@ class CategoryControllerTest extends TestCase
/** @test */ /** @test */
public function it_can_update_an_existing_category() public function it_can_update_an_existing_category()
{ {
// Retrieve an existing category from the database // Retrieve an existing category from the database
$category = Category::latest()->first(); $category = Category::latest()->first();
@@ -173,11 +179,12 @@ class CategoryControllerTest extends TestCase
/** @test */ /** @test */
public function it_cannot_update_an_existing_category() public function it_cannot_update_an_existing_category()
{ {
// Retrieve an existing category from the database // Retrieve an existing category from the database
$category = Category::latest()->first(); $category = Category::latest()->first();
$data = [ $data = [
'name' => 'Greetings', 'name' => 'Updated Category Name',
'description' => 'Updated Description', 'description' => 'Updated Description',
]; ];
@@ -193,10 +200,11 @@ class CategoryControllerTest extends TestCase
/** @test */ /** @test */
public function it_can_delete_a_category() public function it_can_delete_a_category()
{ {
// Create a category // Create a category
$category = Category::latest()->first(); $category = Category::latest()->first();
// Create a related relationship (you may need to adjust this based on your actual relationships) // Create a related relationship
$relation = Relationship::find($category->id); $relation = Relationship::find($category->id);
// Call the destroy method with the category ID // Call the destroy method with the category ID
@@ -209,25 +217,7 @@ class CategoryControllerTest extends TestCase
$response->assertRedirect(); $response->assertRedirect();
// Assert that the response has a success message // 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'));
} }
/** @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'));
}
} }

View File

@@ -7,13 +7,13 @@ use App\Model\kb\Page;
use App\User; use App\User;
use Faker\Factory as FakerFactory; use Faker\Factory as FakerFactory;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Tests\TestCase; use Tests\TestCase;
class PageControllerTest extends TestCase class PageControllerTest extends TestCase
{ {
use DatabaseTransactions;
protected $user; // Declare a user property protected $user; // Declare a user property
// Set up the authenticated user before each test // Set up the authenticated user before each test
@@ -55,6 +55,7 @@ class PageControllerTest extends TestCase
/** @test */ /** @test */
public function it_can_display_the_page_index_page() public function it_can_display_the_page_index_page()
{ {
$this->setUp();
$response = $this->get(route('page.index')); $response = $this->get(route('page.index'));
$response->assertStatus(200); $response->assertStatus(200);
@@ -62,6 +63,7 @@ class PageControllerTest extends TestCase
public function testCreateMethod() public function testCreateMethod()
{ {
$this->setUp();
$response = $this->get('/page/create'); $response = $this->get('/page/create');
$response->assertStatus(200); $response->assertStatus(200);
@@ -69,6 +71,7 @@ class PageControllerTest extends TestCase
public function testValidationPasses() public function testValidationPasses()
{ {
$this->setUp();
$data = [ $data = [
'name' => 'New Page', 'name' => 'New Page',
'description' => 'Page Description', 'description' => 'Page Description',
@@ -87,6 +90,7 @@ class PageControllerTest extends TestCase
public function testValidationFailsWhenNameMissing() public function testValidationFailsWhenNameMissing()
{ {
$this->setUp();
$data = [ $data = [
'description' => 'Page Description', 'description' => 'Page Description',
]; ];
@@ -100,8 +104,9 @@ class PageControllerTest extends TestCase
public function testValidationFailsWhenNameNotUnique() public function testValidationFailsWhenNameNotUnique()
{ {
$this->setUp();
$data = [ $data = [
'name' => 'Page1', 'name' => 'New Page',
'description' => 'Page Description', 'description' => 'Page Description',
]; ];
@@ -114,6 +119,7 @@ class PageControllerTest extends TestCase
public function testValidationFailsWhenDescriptionMissing() public function testValidationFailsWhenDescriptionMissing()
{ {
$this->setUp();
$data = [ $data = [
'name' => 'New', 'name' => 'New',
]; ];
@@ -127,6 +133,7 @@ class PageControllerTest extends TestCase
public function testEditPage() public function testEditPage()
{ {
$this->setUp();
$page = Page::latest()->first(); $page = Page::latest()->first();
$response = $this->get('/page/'.$page->id.'/edit'); $response = $this->get('/page/'.$page->id.'/edit');
@@ -136,6 +143,7 @@ class PageControllerTest extends TestCase
public function testUpdatePage() public function testUpdatePage()
{ {
$this->setUp();
$page = Page::latest()->first(); $page = Page::latest()->first();
$data = [ $data = [
@@ -155,10 +163,11 @@ class PageControllerTest extends TestCase
public function testCannotUpdatePage() public function testCannotUpdatePage()
{ {
$this->setUp();
$page = Page::latest()->first(); $page = Page::latest()->first();
$data = [ $data = [
'name' => 'Page1', 'name' => 'Updated Page Name',
'description' => 'Updated Description', 'description' => 'Updated Description',
]; ];
@@ -173,6 +182,7 @@ class PageControllerTest extends TestCase
public function testDestroyMethod() public function testDestroyMethod()
{ {
$this->setUp();
$page = Page::latest()->first(); $page = Page::latest()->first();
$response = $this->delete('/page/'.$page->id); $response = $this->delete('/page/'.$page->id);

View File

@@ -5,6 +5,8 @@ namespace Tests\Unit;
use App\Model\helpdesk\Ticket\Ticket_Thread; use App\Model\helpdesk\Ticket\Ticket_Thread;
use App\Model\helpdesk\Ticket\Tickets; use App\Model\helpdesk\Ticket\Tickets;
use App\User; use App\User;
use DateTimeZone;
use Faker\Factory as FakerFactory;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
@@ -12,80 +14,45 @@ use Tests\TestCase;
class TicketControllerTest extends TestCase class TicketControllerTest extends TestCase
{ {
use DatabaseTransactions; //Testing Reply Alert and Last Activity filed
public function test_reply()
/**
* A basic unit test example.
*
* @return void
*/
public function test_user_change_the_status()
{ {
$str = 'Demopass@1'; $faker = FakerFactory::create();
$password = Hash::make($str);
$user = new User([
'first_name' => 'a',
'last_name' => 'noor',
'email' => 'naveen12@gmail.com',
'user_name' => 'noor',
'password' => $password,
'active' => 1,
'role' => 'user',
]);
$user->save();
// Authenticate as the created user // Get previously created user to authenticate
$user = User::latest()->first();
$this->actingAs($user); $this->actingAs($user);
$ticket = new Tickets( $this->assertAuthenticated();
[
'ticket_number' => 'AAAA-0000-0001',
'user_id' => $user->id,
'priority_id' => 2,
'sla' => 2,
'help_topic_id' => 1,
'status' => 1,
'source' => 1,
]
);
$ticket->save(); //Get previously created Ticket
$ticket->dept_id = 1;
$ticket->save();
$ticket_thread = new Ticket_Thread( $tickets = Tickets::latest()->first();
[
'ticket_id' => $ticket->id,
'user_id' => $user->id,
'poster' => 'client',
'title' => 'TestCase',
'body' => 'Testing',
]
);
$ticket_thread->save();
$mytickets = $this->get(route('ticket2')); // Define the route URL with the Ticket ID
$mytickets->assertStatus(200);
$response = $this->post(route('select_all'), [ $url = route('ticket.thread', ['id' => $tickets->id]);
'select_all' => [$ticket->id],
'submit' => 'Open',
]); $response2 = $this->get($url);
// Assert that the response status code indicates success // Assert that the response status is 200 (OK).
$response->assertStatus(302); // Adjust this as needed $response2->assertStatus(200);
// Assert that the ticket's status has been updated to open // Create fake data for the reply
$replyData = [
'ticket_ID' => $tickets->id,
'reply_content' => $faker->paragraph,
'created_at' => date_default_timezone_set('UTC'),
'updated_at' => date_default_timezone_set('UTC'),
];
// Make a POST request to the route with the reply data
$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'));
$response->assertSessionHas('success', Lang::get('lang.tickets_have_been_opened'));
$response = $this->post(route('select_all'), [
'select_all' => [$ticket->id],
'submit' => 'Close',
]);
$response->assertStatus(302); // Adjust this as needed
$this->assertEquals(3, $ticket->fresh()->status); // Adjust this as needed
$response->assertSessionHas('success', Lang::get('lang.tickets_have_been_closed'));
} }
} }