Completed
Pull Request — master (#7)
by
unknown
02:33
created

SettingsFieldTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 57
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorThrowsExceptionWhenGivenString() 0 3 1
A testConstructorThrowsExceptionWhenGivenChildren() 0 3 1
B testFieldListGeneration() 0 29 2
A testFieldAddsJavascriptRequirements() 0 8 1
1
<?php
2
3
namespace SilverStripe\DocumentConverter\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\DocumentConverter\SettingsField;
7
use SilverStripe\DocumentConverter\ImportField;
8
use SilverStripe\Forms\CheckboxField;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\HeaderField;
12
use SilverStripe\Forms\TreeDropdownField;
13
use SilverStripe\View\Requirements;
14
15
class SettingsFieldTest extends SapphireTest
16
{
17
    /**
18
     * @expectedException InvalidArgumentException
19
     */
20
    public function testConstructorThrowsExceptionWhenGivenString()
21
    {
22
        new SettingsField('exception time!');
23
    }
24
25
    /**
26
     * @expectedException InvalidArgumentException
27
     */
28
    public function testConstructorThrowsExceptionWhenGivenChildren()
29
    {
30
        new SettingsField(['i', 'don\'t', 'like', 'kids']);
31
    }
32
33
    public function testFieldAddsJavascriptRequirements()
34
    {
35
        // Start with a clean slate (no global state interference)
36
        Requirements::backend()->clear();
37
38
        new SettingsField();
39
        $javascript = Requirements::backend()->getJavascript();
40
        $this->assertNotEmpty($javascript);
41
    }
42
43
    public function testFieldListGeneration()
44
    {
45
        $importField = new SettingsField();
46
47
        $fields = $importField->getChildren();
48
        $this->assertInstanceOf(FieldList::class, $fields);
49
50
        // We don't need to check that all of the fields are there, but just check a couple
51
        $this->assertInstanceOf(HeaderField::class, $fields->fieldByName('FileWarningHeader'));
52
        $innerField = $fields->fieldByName('ImportedFromFile');
53
        $this->assertInstanceOf(ImportField::class, $innerField);
54
55
        // Check the getter works
56
        $this->assertSame($innerField, $importField->getInnerField());
57
58
        // Check the fields have been given has the change tracker disabled
59
        $settingsFields = [
60
            'SplitHeader' => DropdownField::class,
61
            'KeepSource' => CheckboxField::class,
62
            'ChosenFolderID' => TreeDropdownField::class,
63
            'IncludeTOC' => CheckboxField::class,
64
            'PublishPages' => CheckboxField::class
65
        ];
66
        foreach($settingsFields as $fieldName => $className) {
67
            $field = $fields->fieldByName(
68
                'DocumentConversionSettings-' . $fieldName
69
            );
70
            $this->assertInstanceOf($className, $field);
71
            $this->assertContains('no-change-track', $field->extraClass());
72
        }
73
    }
74
}
75