Completed
Push — master ( c2bc77...f2e185 )
by Jonathan
08:09
created

testMandatoryModuleActivatonCannotBeChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 25
rs 9.8666
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Testing\RefreshDatabase 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...
7
use Uccello\Core\Models\Module;
8
use Uccello\Core\Models\Domain;
9
10
class ModuleManagerTest extends TestCase
11
{
12
    use RefreshDatabase;
13
14
    /**
15
     * Acting as admin
16
     *
17
     * @return void
18
     */
19
    protected function actingAsAdmin()
20
    {
21
        $user = factory(\Uccello\Core\Models\User::class)->make();
0 ignored issues
show
Bug introduced by
The function factory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $user = /** @scrutinizer ignore-call */ factory(\Uccello\Core\Models\User::class)->make();
Loading history...
22
        $user->is_admin = true;
23
        $user->save();
24
25
        $this->actingAs($user);
26
    }
27
28
    protected function getSettingsModule()
29
    {
30
       return Module::where('name', 'settings')->first();
31
    }
32
33
    public function testModuleActivationCanBeChanged()
34
    {
35
        $this->actingAsAdmin();
36
37
        $domain = Domain::first();
38
39
        // Create fake module
40
        $module = factory(\Uccello\Core\Models\Module::class)->create();
0 ignored issues
show
Bug introduced by
The function factory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $module = /** @scrutinizer ignore-call */ factory(\Uccello\Core\Models\Module::class)->create();
Loading history...
41
        $moduleName = $module->name;
42
43
        $url = ucroute('uccello.settings.module.activation', $domain);
44
45
        // Deactivate module
46
        $response = $this->post($url, [
47
            'src_module' => $moduleName,
48
            'active' => 0
49
        ]);
50
51
        // Test response content
52
        $response->assertJson([ 'success' => true, 'message' => uctrans('message.module_deactivated', $this->getSettingsModule()) ]);
53
54
        // Activation status wash changed
55
        $module = $module->refresh();
56
        $this->assertFalse($module->isActiveOnDomain($domain));
57
58
        // Activate module
59
        $response = $this->json('POST', $url, [
60
            'src_module' => $moduleName,
61
            'active' => 1
62
        ]);
63
64
        // Test response content
65
        $response->assertJson([ 'success' => true, 'message' => uctrans('message.module_activated', $this->getSettingsModule()) ]);
66
67
        // Test if activation status was changed
68
        $module = $module->refresh();
69
        $this->assertTrue($module->isActiveOnDomain($domain));
70
    }
71
72
    public function testMandatoryModuleActivationCannotBeChanged()
73
    {
74
        $this->actingAsAdmin();
75
76
        $domain = Domain::first();
77
78
        $moduleName = 'settings';
79
        $module = Module::where('name', $moduleName)->first();
80
81
        $this->assertTrue($module->isMandatory());
82
83
        $url = ucroute('uccello.settings.module.activation', $domain);
84
85
        // Try to deactivate module
86
        $response = $this->json('POST', $url, [
87
            'src_module' => $moduleName,
88
            'active' => 0
89
        ]);
90
91
        // Test response content
92
        $response->assertJson([ 'success' => false, 'error' => uctrans('error.module_is_mandatory', $this->getSettingsModule()) ]);
93
94
        // Test if activation status was not changed
95
        $module = $module->refresh();
96
        $this->assertTrue($module->isActiveOnDomain($domain));
97
    }
98
99
    public function testModuleNotDefined()
100
    {
101
        $this->actingAsAdmin();
102
103
        $domain = Domain::first();
104
105
        $url = ucroute('uccello.settings.module.activation', $domain);
106
107
        // Try to deactivate module
108
        $response = $this->json('POST', $url, [
109
            'active' => 0
110
        ]);
111
112
        // Test response content
113
        $response->assertJson([ 'success' => false, 'error' => uctrans('error.module_not_defined', $this->getSettingsModule()) ]);
114
    }
115
}
116