Passed
Pull Request — master (#12)
by Robbie
04:58 queued 01:01
created

PopulateThemeSampleDataTaskTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

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