Passed
Push — master ( 9f8a04...cca879 )
by Schlaefer
11:04 queued 04:08
created

CategoriesControllerTest::testDeleteGetForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Test\TestCase\Controller\Admin;
14
15
use Cake\ORM\TableRegistry;
16
use Saito\Test\IntegrationTestCase;
17
18
/**
19
 * Class CategoriesControllerTest
20
 *
21
 * @package App\Test\TestCase\Controller\Admin
22
 * @group App\Test\TestCase\Controller\Admin
23
 */
24
class CategoriesControllerTest extends IntegrationTestCase
25
{
26
27
    public $fixtures = [
28
        'app.Category',
29
        'app.Entry',
30
        'app.Setting',
31
        'app.User',
32
        'app.UserBlock',
33
        'app.UserIgnore',
34
        'app.UserRead',
35
        'app.UserOnline',
36
    ];
37
38
    public function setUp()
39
    {
40
        parent::setUp();
41
        foreach (['Entries', 'Categories'] as $table) {
42
            $this->$table = TableRegistry::get($table);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

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

42
            $this->$table = /** @scrutinizer ignore-deprecated */ TableRegistry::get($table);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
        }
44
    }
45
46
    /**
47
     * Tests viewing empty "add new category" form
48
     */
49
    public function testAddGet()
50
    {
51
        $this->mockSecurity();
52
        $this->_loginUser(1);
53
54
        $this->get('/admin/categories/add');
55
56
        $this->assertResponseOk();
57
58
        $category = $this->viewVariable('category');
59
60
        // default accession for new categories should be set to logged-in user (1)
61
        $this->assertEquals(1, $category->get('accession'));
62
        $this->assertEquals(1, $category->get('accession_new_thread'));
63
        $this->assertEquals(1, $category->get('accession_new_posting'));
64
    }
65
66
    /**
67
     * delete category and postings
68
     */
69
    public function testDeleteDelete()
70
    {
71
        $this->mockSecurity();
72
        $this->_loginUser(1);
73
        $source = 2;
74
75
        $readPostings = function () use ($source) {
76
            $read = [];
77
            $read['all'] = $this->Entries->find()->all()->count();
0 ignored issues
show
Bug Best Practice introduced by
The property Entries does not exist on App\Test\TestCase\Contro...ategoriesControllerTest. Did you maybe forget to declare it?
Loading history...
78
            $read['source'] = $this->Entries->find()
79
                ->where(['category_id' => $source])
80
                ->count();
81
82
            return $read;
83
        };
84
85
        $this->assertTrue($this->Categories->exists($source));
0 ignored issues
show
Bug Best Practice introduced by
The property Categories does not exist on App\Test\TestCase\Contro...ategoriesControllerTest. Did you maybe forget to declare it?
Loading history...
86
        $before = $readPostings();
87
        $this->assertGreaterThan(0, $before['source']);
88
89
        $data = ['mode' => 'delete'];
90
        $this->post('/admin/categories/delete/2', $data);
91
92
        $this->assertFalse($this->Categories->exists($source));
93
        $this->assertRedirect('/admin/categories');
94
95
        $after = $readPostings();
96
        $this->assertEquals(0, $after['source']);
97
        $expected = $before['all'] - $before['source'];
98
        $this->assertEquals($expected, $after['all']);
99
    }
100
101
    /**
102
     * Test showing of delete form
103
     *
104
     * @return void
105
     */
106
    public function testDeleteGetForm(): void
107
    {
108
        $this->_loginUser(1);
109
        $source = 2;
0 ignored issues
show
Unused Code introduced by
The assignment to $source is dead and can be removed.
Loading history...
110
        $target = 4;
0 ignored issues
show
Unused Code introduced by
The assignment to $target is dead and can be removed.
Loading history...
111
112
        $this->get('/admin/categories/delete/2');
113
114
        $targetCategories = $this->viewVariable('targetCategories');
115
        $this->assertCount(4, $targetCategories);
116
        $this->assertArraySubset([4 => 'Offtopic', 5 => 'Trash'], $targetCategories);
0 ignored issues
show
Bug introduced by
It seems like $targetCategories can also be of type Countable and Traversable; however, parameter $array of PHPUnit\Framework\Assert::assertArraySubset() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

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

116
        $this->assertArraySubset([4 => 'Offtopic', 5 => 'Trash'], /** @scrutinizer ignore-type */ $targetCategories);
Loading history...
117
    }
118
119
    /**
120
     * delete category and merge postings into other category
121
     */
122
    public function testDeleteMerge()
123
    {
124
        $this->mockSecurity();
125
        $this->_loginUser(1);
126
        $source = 2;
127
        $target = 4;
128
129
        $readPostings = function () use ($source, $target) {
130
            $read = [];
131
            $read['all'] = $this->Entries->find()->all()->count();
0 ignored issues
show
Bug Best Practice introduced by
The property Entries does not exist on App\Test\TestCase\Contro...ategoriesControllerTest. Did you maybe forget to declare it?
Loading history...
132
            $read['source'] = $this->Entries->find()
133
                ->where(['category_id' => $source])
134
                ->count();
135
            $read['target'] = $this->Entries->find()
136
                ->where(['category_id' => $target])
137
                ->count();
138
139
            return $read;
140
        };
141
142
        $this->assertTrue($this->Categories->exists($source));
0 ignored issues
show
Bug Best Practice introduced by
The property Categories does not exist on App\Test\TestCase\Contro...ategoriesControllerTest. Did you maybe forget to declare it?
Loading history...
143
        $this->assertTrue($this->Categories->exists($target));
144
        $before = $readPostings();
145
        $this->assertGreaterThan(0, $before['source']);
146
        $this->assertGreaterThan(0, $before['target']);
147
148
        $data = ['mode' => 'move', 'targetCategory' => $target];
149
        $this->post('/admin/categories/delete/2', $data);
150
151
        // old category removed
152
        $this->assertFalse($this->Categories->exists($source));
153
        // target category not eaten
154
        $this->assertTrue($this->Categories->exists($target));
155
156
        $after = $readPostings();
157
        // no posting in old category
158
        $this->assertEquals(0, $after['source']);
159
        // postings are moved to new category
160
        $expected = $before['target'] + $before['source'];
161
        $this->assertEquals($expected, $after['target']);
162
        // no post is lost
163
        $this->assertEquals($before['all'], $after['all']);
164
165
        $this->assertRedirect('/admin/categories');
166
    }
167
}
168