Skip to content

Commit c24b343

Browse files
committed
Initial tickets tests
1 parent 9355ffd commit c24b343

4 files changed

Lines changed: 254 additions & 0 deletions

File tree

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="bootstrap/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="PanicHD Test Suite">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<php>
17+
<env name="APP_ENV" value="testing"/>
18+
<env name="CACHE_DRIVER" value="array"/>
19+
<env name="SESSION_DRIVER" value="array"/>
20+
<env name="QUEUE_DRIVER" value="sync"/>
21+
</php>
22+
</phpunit>

src/Models/Ticket.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,18 @@ public function scopeInCategory($query, $id = null)
766766
}
767767
}
768768

769+
/**
770+
* Get tickets that are hidden for users
771+
*
772+
* @param $query
773+
*
774+
* @return mixed
775+
*/
776+
public function scopeHidden($query)
777+
{
778+
return $query->where('hidden', '1');
779+
}
780+
769781
/**
770782
* Get tickets that are not hidden for users
771783
*

tests/Feature/TicketsTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace PanicHD\PanicHD\Tests\Feature;
4+
5+
use PanicHD\PanicHD\Models\Ticket;
6+
use PanicHD\PanicHD\Tests\TestCase;
7+
8+
class TicketsTest extends TestCase
9+
{
10+
/*
11+
* Check that we don't have access without auth() Member
12+
*/
13+
public function testNoAuth()
14+
{
15+
$this->load_vars();
16+
17+
if ($this->status == "Installed" and !is_null($this->main_route)){
18+
// Main route
19+
$response = $this->get($this->main_route);
20+
$response->assertRedirect('/login');
21+
22+
// Ticket creation
23+
$response = $this->get(route($this->main_route . '.create'));
24+
$response->assertRedirect('/login');
25+
}
26+
}
27+
28+
/*
29+
* Access to the main route
30+
*/
31+
public function testMainRoute()
32+
{
33+
$this->load_vars();
34+
35+
if ($this->status == "Installed" and !is_null($this->main_route)){
36+
// Member access
37+
if(!is_null($this->member)){
38+
$response = $this->actingAs($this->member)->get($this->main_route);
39+
$response->assertStatus(200);
40+
}
41+
42+
// Agent access
43+
if(!is_null($this->agent)){
44+
$response = $this->actingAs($this->agent)->get($this->main_route);
45+
$response->assertStatus(200);
46+
}
47+
48+
// Admin access
49+
if(!is_null($this->admin)){
50+
$response = $this->actingAs($this->admin)->get($this->main_route);
51+
$response->assertStatus(200);
52+
}
53+
}
54+
}
55+
56+
/*
57+
* Ticket creation
58+
*/
59+
public function testTicketCreate()
60+
{
61+
$this->load_vars();
62+
63+
if ($this->status == "Installed" and !is_null($this->main_route)){
64+
// Member access
65+
if(!is_null($this->member)){
66+
$response = $this->actingAs($this->member)->get(route($this->main_route . '.create'));
67+
}
68+
69+
// Agent access
70+
if(!is_null($this->agent)){
71+
$response = $this->actingAs($this->agent)->get(route($this->main_route . '.create'));
72+
}
73+
74+
// Admin access
75+
if(!is_null($this->admin)){
76+
$response = $this->actingAs($this->admin)->get(route($this->main_route . '.create'));
77+
}
78+
}
79+
}
80+
81+
/*
82+
* Access a Ticket Card
83+
*/
84+
public function testTicketShow()
85+
{
86+
$this->load_vars();
87+
88+
if ($this->status == "Installed" and !is_null($this->main_route)){
89+
// Member access
90+
if(!is_null($this->member)){
91+
// Visible ticket
92+
$ticket = clone $this->member_tickets_builder;
93+
$ticket = $ticket->notHidden()->first();
94+
$response = $this->actingAs($this->member)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
95+
$response->assertStatus(200);
96+
97+
// Hidden ticket without $this->member in notifications
98+
$ticket = clone $this->member_tickets_builder;
99+
$member = $this->member;
100+
$ticket = $ticket->hidden()
101+
->whereDoesntHave('commentNotifications', function($query)use($member){
102+
$query->where('member_id', $member->id);
103+
})
104+
->first();
105+
if(!is_null($ticket)){
106+
$response = $this->actingAs($this->member)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
107+
$response->assertStatus(302);
108+
}
109+
}
110+
111+
// Agent access
112+
if(!is_null($this->agent)){
113+
$ticket = $this->agent->agentTickets()->inRandomOrder()->first();
114+
115+
$response = $this->actingAs($this->agent)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
116+
$response->assertStatus(200);
117+
}
118+
119+
// Admin access
120+
if(!is_null($this->admin)){
121+
$ticket_build = Ticket::inRandomOrder();
122+
123+
// Newest ticket
124+
$build = clone $ticket_build;
125+
$ticket = $build->newest()->first();
126+
127+
if (!is_null($ticket)){
128+
$response = $this->actingAs($this->admin)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
129+
$response->assertStatus(200);
130+
}
131+
132+
// Active ticket
133+
$build = clone $ticket_build;
134+
$ticket = $build->active()->first();
135+
136+
if (!is_null($ticket)){
137+
$response = $this->actingAs($this->admin)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
138+
$response->assertStatus(200);
139+
}
140+
141+
// Complete ticket
142+
$build = clone $ticket_build;
143+
$ticket = $build->complete()->first();
144+
145+
if (!is_null($ticket)){
146+
$response = $this->actingAs($this->admin)->get(route($this->main_route . '.show', ['id' => $ticket->id]));
147+
$response->assertStatus(200);
148+
}
149+
}
150+
}
151+
}
152+
}

tests/TestCase.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace PanicHD\PanicHD\Tests;
4+
5+
use PanicHD\PanicHD\Controllers\InstallController;
6+
use PanicHD\PanicHD\Models\Member;
7+
use PanicHD\PanicHD\Models\Setting;
8+
use Tests\TestCase as Base;
9+
10+
class TestCase extends Base
11+
{
12+
// Package general status
13+
protected $status = "Not installed";
14+
15+
// Main route
16+
protected $main_route = null;
17+
18+
// Member types tests set
19+
protected $member = null, $agent = null, $admin = null;
20+
21+
// Eloquent reusable builder for Member Tickets
22+
protected $member_tickets_builder;
23+
24+
/*
25+
* Load a basic vars set for package tests
26+
*/
27+
public function load_vars()
28+
{
29+
if (is_null($this->main_route)){
30+
$InstallController = new InstallController();
31+
32+
if (!$InstallController->isInstalled()){
33+
if (\Cache::has('panichd::installation')) $this->status = "Installing";
34+
35+
}elseif (!$InstallController->isUpdated()){
36+
$this->status = "Out of date";
37+
38+
}else{
39+
$this->status = "Installed";
40+
41+
$this->main_route = Setting::grab('main_route');
42+
}
43+
}
44+
45+
if (is_null($this->member)){
46+
if (Member::users()->count() > 0){
47+
$this->member = Member::whereHas('tickets', function($query){ $query->notHidden(); })->inRandomOrder()->users()->first();
48+
if (!is_null($this->member)){
49+
$this->member_tickets_builder = $this->member->tickets()->inRandomOrder();
50+
}
51+
}
52+
}
53+
54+
if (is_null($this->agent)){
55+
if (Member::agents()->count() > 0){
56+
$this->agent = Member::whereHas('agentTickets')->inRandomOrder()->agents()->first();
57+
}
58+
}
59+
60+
if (is_null($this->admin)){
61+
if (Member::admins()->count() > 0){
62+
$this->admin = Member::inRandomOrder()->admins()->first();
63+
}
64+
}
65+
}
66+
}
67+
68+
?>

0 commit comments

Comments
 (0)