testMandatoryModuleActivationCannotBeChanged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 9.8666
1
<?php
2
3
namespace Uccello\Core\Tests\Feature;
4
5
use Tests\TestCase;
0 ignored issues
show
Bug introduced by
The type Tests\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Foundation\Testing\RefreshDatabase;
7
use Illuminate\Support\Facades\Artisan;
8
use Uccello\Core\Models\Module;
9
use Uccello\Core\Models\Domain;
10
11
class ModuleManagerTest extends TestCase
12
{
13
    use RefreshDatabase;
14
15
    /**
16
     * Acting as admin
17
     *
18
     * @return void
19
     */
20
    protected function actingAsAdmin()
21
    {
22
        $user = factory(\Uccello\Core\Models\User::class)->make();
23
        $user->is_admin = true;
24
        $user->save();
25
26
        Artisan::call('cache:clear');
27
28
        $this->actingAs($user);
29
    }
30
31
    protected function getSettingsModule()
32
    {
33
       return Module::where('name', 'settings')->first();
34
    }
35
36
    public function testModuleActivationCanBeChanged()
37
    {
38
        $this->actingAsAdmin();
39
40
        $domain = Domain::first();
41
42
        // Create fake module
43
        $module = factory(\Uccello\Core\Models\Module::class)->create();
44
        $moduleName = $module->name;
45
46
        $url = ucroute('uccello.settings.module.activation', $domain);
47
48
        // Deactivate module
49
        $response = $this->post($url, [
50
            'src_module' => $moduleName,
51
            'active' => 0
52
        ]);
53
54
        // Test response content
55
        $response->assertJson([ 'success' => true, 'message' => uctrans('message.module_deactivated', $this->getSettingsModule()) ]);
56
57
        // Activation status wash changed
58
        $module = $module->refresh();
59
        $this->assertFalse($module->isActiveOnDomain($domain));
60
61
        // Activate module
62
        $response = $this->json('POST', $url, [
63
            'src_module' => $moduleName,
64
            'active' => 1
65
        ]);
66
67
        // Test response content
68
        $response->assertJson([ 'success' => true, 'message' => uctrans('message.module_activated', $this->getSettingsModule()) ]);
69
70
        // Test if activation status was changed
71
        $module = $module->refresh();
72
        $this->assertTrue($module->isActiveOnDomain($domain));
73
    }
74
75
    public function testMandatoryModuleActivationCannotBeChanged()
76
    {
77
        $this->actingAsAdmin();
78
79
        $domain = Domain::first();
80
81
        $moduleName = 'settings';
82
        $module = Module::where('name', $moduleName)->first();
83
84
        $this->assertTrue($module->isMandatory());
85
86
        $url = ucroute('uccello.settings.module.activation', $domain);
87
88
        // Try to deactivate module
89
        $response = $this->json('POST', $url, [
90
            'src_module' => $moduleName,
91
            'active' => 0
92
        ]);
93
94
        // Test response content
95
        $response->assertJson([ 'success' => false, 'error' => uctrans('error.module_is_mandatory', $this->getSettingsModule()) ]);
96
97
        // Test if activation status was not changed
98
        $module = $module->refresh();
99
        $this->assertTrue($module->isActiveOnDomain($domain));
100
    }
101
102
    public function testModuleNotDefined()
103
    {
104
        $this->actingAsAdmin();
105
106
        $domain = Domain::first();
107
108
        $url = ucroute('uccello.settings.module.activation', $domain);
109
110
        // Try to deactivate module
111
        $response = $this->json('POST', $url, [
112
            'active' => 0
113
        ]);
114
115
        // Test response content
116
        $response->assertJson([ 'success' => false, 'error' => uctrans('error.module_not_defined', $this->getSettingsModule()) ]);
117
    }
118
}
119