PopulateThemeSampleDataTaskTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnlyCreateContactFormOnce() 0 27 2
A bufferedTask() 0 5 1
1
<?php
2
3
namespace CWP\CWP\Tests\Tasks;
4
5
use CWP\CWP\Tasks\PopulateThemeSampleDataTask;
6
use SilverStripe\UserForms\Model\UserDefinedForm;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\Model\UserDefinedForm 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 SilverStripe\Dev\BuildTask;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Dev\SapphireTest;
10
11
class PopulateThemeSampleDataTaskTest extends SapphireTest
12
{
13
    protected $usesDatabase = true;
14
15
    /**
16
     * Ensure that the "contact" user form is only created once
17
     */
18
    public function testOnlyCreateContactFormOnce()
19
    {
20
        if (!class_exists(UserDefinedForm::class)) {
21
            $this->markTestSkipped('This test requires the userforms module to be installed');
22
        }
23
24
        $createdMessage = 'Created "contact" UserDefinedForm';
25
26
        $task = new PopulateThemeSampleDataTask;
27
28
        // Run the task
29
        $this->assertContains($createdMessage, $this->bufferedTask($task));
30
31
        // Run a second time
32
        $this->assertNotContains($createdMessage, $this->bufferedTask($task));
33
34
        // Change the page name
35
        $form = UserDefinedForm::get()->filter('URLSegment', 'contact')->first();
36
        $form->URLSegment = 'testing';
37
        $form->write();
38
39
        // Ensure the old version is still detected in draft, so not recreated
40
        $this->assertNotContains($createdMessage, $this->bufferedTask($task));
41
42
        // Delete the page, then ensure it's recreated again (DataObject::delete will remove staged versions)
43
        $form->delete();
44
        $this->assertContains($createdMessage, $this->bufferedTask($task));
45
    }
46
47
    /**
48
     * Run a BuildTask while buffering its output, and return the result
49
     *
50
     * @param  BuildTask $task
51
     * @return string
52
     */
53
    protected function bufferedTask(BuildTask $task)
54
    {
55
        ob_start();
56
        $task->run(new HTTPRequest('GET', '/'));
57
        return ob_get_clean();
58
    }
59
}
60