MigrateToDocumentSetsTaskTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 140
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanOutputToCliOrBrowser() 0 19 2
A outputProvider() 0 7 1
A testShowErrorOnInvalidAction() 0 5 1
A testCreateDefaultDocumentSets() 0 20 1
A testReassignDocumentsToFirstSet() 0 22 1
A runTask() 0 9 1
A fixtureOldRelations() 0 19 3
1
<?php
2
3
class MigrateToDocumentSetsTaskTest extends SapphireTest
4
{
5
    protected static $fixture_file = 'MigrateToDocumentSetsTaskTest.yml';
6
7
    /**
8
     * Ensure that output is formatted either for the CLI or browser
9
     *
10
     * @param bool   $isCli
11
     * @param string $expected
12
     * @dataProvider outputProvider
13
     */
14
    public function testCanOutputToCliOrBrowser($isCli, $expected)
15
    {
16
        $lines = array('Test', 'Test line 2');
17
18
        $mock = $this->getMockBuilder('MigrateToDocumentSetsTask')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
19
            ->setMethods(array('isCli'))
20
            ->getMock();
21
22
        $mock->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method exactly() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
23
            ->method('isCli')
24
            ->will($this->returnValue($isCli));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        ob_start();
27
        foreach ($lines as $line) {
28
            $mock->output($line);
29
        }
30
        $result = ob_get_clean();
31
        $this->assertSame($expected, $result);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
    }
33
34
    /**
35
     * @return array[]
36
     */
37
    public function outputProvider()
38
    {
39
        return array(
40
            array(true, 'Test' . PHP_EOL . 'Test line 2' . PHP_EOL),
41
            array(false, 'Test<br />Test line 2<br />')
42
        );
43
    }
44
45
    /**
46
     * Ensure that providing an invalid action returns an error
47
     */
48
    public function testShowErrorOnInvalidAction()
49
    {
50
        $result = $this->runTask(array('action' => 'coffeetime'));
51
        $this->assertContains('Error! Specified action is not valid.', $result);
52
    }
53
54
    /**
55
     * Test that default document sets can be created for those pages that don't have them already
56
     */
57
    public function testCreateDefaultDocumentSets()
58
    {
59
        $this->fixtureOldRelations();
60
61
        $result = $this->runTask(array('action' => 'create-default-document-set'));
62
        $this->assertContains('Finished', $result);
63
        // There are four pages in the fixture, but one of them already has a document set, so should be unchanged
64
        $this->assertContains('Default document set added: 3', $result);
65
        $this->assertContains('Skipped: already has a set: 1', $result);
66
67
        // Test that some of the relationship records were written correctly
68
        $this->assertCount(1, $firstPageSets = $this->objFromFixture('SiteTree', 'one')->getDocumentSets());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        $this->assertSame('Default', $firstPageSets->first()->Title);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $this->assertCount(1, $this->objFromFixture('SiteTree', 'two')->getDocumentSets());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
        // With dryrun enabled and being run the second time, nothing should be done
73
        $result = $this->runTask(array('action' => 'create-default-document-set', 'dryrun' => '1'));
74
        $this->assertContains('Skipped: already has a set: 4', $result);
75
        $this->assertContains('NOTE: Dryrun mode enabled', $result);
76
    }
77
78
    /**
79
     * Test that legacy ORM relationship maps are migrated to the new page -> document set -> document relationship
80
     */
81
    public function testReassignDocumentsToFirstSet()
82
    {
83
        $this->fixtureOldRelations();
84
85
        // Ensure default sets are created
86
        $this->runTask(array('action' => 'create-default-document-set'));
87
88
        // Dryrun check
89
        $result = $this->runTask(array('action' => 'reassign-documents', 'dryrun' => '1'));
90
        $this->assertContains('NOTE: Dryrun mode enabled', $result);
91
        $this->assertContains('Reassigned to document set: 3', $result);
92
93
        // Actual run
94
        $result = $this->runTask(array('action' => 'reassign-documents'));
95
        $this->assertNotContains('NOTE: Dryrun mode enabled', $result);
96
        $this->assertContains('Reassigned to document set: 3', $result);
97
98
        // Smoke ORM checks
99
        $this->assertCount(1, $this->objFromFixture('SiteTree', 'one')->getAllDocuments());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        $this->assertCount(1, $this->objFromFixture('SiteTree', 'two')->getAllDocuments());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        $this->assertCount(0, $this->objFromFixture('SiteTree', 'four')->getAllDocuments());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MigrateToDocumentSetsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
    }
103
104
    /**
105
     * Centralises (slightly) logic for capturing direct output from the task
106
     *
107
     * @param  array $getVars
108
     * @return string Task output
109
     */
110
    protected function runTask(array $getVars)
111
    {
112
        $task = new MigrateToDocumentSetsTask;
113
        $request = new SS_HTTPRequest('GET', '/', $getVars);
114
115
        ob_start();
116
        $task->run($request);
117
        return ob_get_clean();
118
    }
119
120
    /**
121
     * Set up the old many many relationship table from documents to pages
122
     */
123
    protected function fixtureOldRelations()
124
    {
125
        if (!DB::get_schema()->hasTable('DMSDocument_Pages')) {
126
            DB::create_table('DMSDocument_Pages', array(
127
                'DMSDocumentID' => 'int(11) null',
128
                'SiteTreeID' => 'int(11) null'
129
            ));
130
        }
131
132
        $documentIds = $this->getFixtureFactory()->getIds('DMSDocument');
133
        $pageIds = $this->getFixtureFactory()->getIds('SiteTree');
134
        foreach (array('one', 'two', 'three') as $fixtureName) {
135
            $this->getFixtureFactory()->createRaw(
136
                'DMSDocument_Pages',
137
                'rln_' . $fixtureName,
138
                array('DMSDocumentID' => $documentIds[$fixtureName], 'SiteTreeID' => $pageIds[$fixtureName])
139
            );
140
        }
141
    }
142
}
143