Completed
Pull Request — master (#119)
by Franco
17:38 queued 01:48
created

StubDocumentSetMockExtension   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 3
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateQueryFields() 0 6 1
1
<?php
2
3
class DMSDocumentSetTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $fixture_file = 'dmstest.yml';
6
7
    protected $extraDataObjects = array(
8
        'DMSDocument_Mock'
9
    );
10
11
    /**
12
     * Ensure that getDocuments is extensible
13
     */
14 View Code Duplication
    public function testGetDocumentsIsExtensible()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        DMSDocumentSet::add_extension('StubRelatedDocumentExtension');
17
18
        $set = new DMSDocumentSet;
19
        $documents = $set->getDocuments();
20
21
        $this->assertCount(1, $documents);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentSetTest>.

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...
22
        $this->assertSame('Extended', $documents->first()->Filename);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentSetTest>.

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
    }
24
25
    /**
26
     * Test that the GridField for documents isn't shown until you've saved the set
27
     */
28
    public function testGridFieldShowsWhenSetIsSaved()
29
    {
30
        $set = DMSDocumentSet::create();
31
32
        // Not in database yet
33
        $fields = $set->getCMSFields();
34
        $this->assertNull($fields->fieldByName('Root.Main.Documents'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
35
        $gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice');
36
        $this->assertNotNull($gridFieldNotice);
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
37
        $this->assertContains('Managing documents will be available', $gridFieldNotice->getContent());
38
39
        // In the database
40
        $set->Title = 'Testing';
41
        $set->write();
42
        $fields = $set->getCMSFields();
43
        $this->assertNotNull($fields->fieldByName('Root.Main.Documents'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
44
        $this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
45
    }
46
47
    public function testRelations()
48
    {
49
        $s1 = $this->objFromFixture('SiteTree', 's1');
50
        $s2 = $this->objFromFixture('SiteTree', 's2');
0 ignored issues
show
Unused Code introduced by
$s2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
        $s4 = $this->objFromFixture('SiteTree', 's4');
52
53
        $ds1 = $this->objFromFixture('DMSDocumentSet', 'ds1');
54
        $ds2 = $this->objFromFixture('DMSDocumentSet', 'ds2');
55
        $ds3 = $this->objFromFixture('DMSDocumentSet', 'ds3');
0 ignored issues
show
Unused Code introduced by
$ds3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
        $this->assertCount(0, $s4->getDocumentSets(), 'Page 4 has no document sets associated');
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentSetTest>.

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...
58
        $this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets');
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentSetTest>.

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...
59
        $this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentSetTest>.

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...
60
    }
61
62
    /**
63
     * Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context.
64
     */
65
    public function testDocumentGridFieldConfig()
66
    {
67
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
68
        $fields = $set->getCMSFields();
69
        $gridField = $fields->fieldByName('Root.Main.Documents');
70
        $this->assertTrue((bool) $gridField->hasClass('documents'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentSetTest>.

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
        /** @var GridFieldConfig $config */
73
        $config = $gridField->getConfig();
74
75
        $this->assertNotNull($config->getComponentByType('DMSGridFieldDeleteAction'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
76
        $this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
77
        $this->assertSame($set->ID, $addNew->getDocumentSetId());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentSetTest>.

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...
78
79
        if (class_exists('GridFieldPaginatorWithShowAll')) {
80
            $this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
81
        } else {
82
            $paginator = $config->getComponentByType('GridFieldPaginator');
83
            $this->assertNotNull($paginator);
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
84
            $this->assertSame(15, $paginator->getItemsPerPage());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentSetTest>.

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...
85
        }
86
87
        $sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull';
88
        $this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows'));
89
    }
90
91
    /**
92
     * Test that query fields can be added to the gridfield
93
     */
94
    public function testAddQueryFields(){
95
96
        /** @var DMSDocumentSet $set */
97
        $set = $this->objFromFixture('DMSDocumentSet', 'ds6');
98
        $doc = new DMSDocument_Mock();
99
        $set->Documents()->add($doc);
0 ignored issues
show
Bug introduced by
The method Documents() does not exist on DMSDocumentSet. Did you maybe mean getDocuments()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
100
        DMSDocumentSet::$linked_document_type = 'DMSDocument_Mock';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'DMSDocument_Mock' of type string is incompatible with the declared type object<Varchar> of property $linked_document_type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
        /** @var FieldList $fields */
102
        $fields = new FieldList(new TabSet('Root'));
103
        /** @var FieldList $fields */
104
        $set->addQueryFields($fields);
105
        $keyValuePairs = $fields->dataFieldByName('KeyValuePairs');
106
        $this->assertNotNull($keyValuePairs,
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
107
            'addQueryFields() includes KeyValuePairs composite field');
108
        $this->assertNotNull($keyValuePairs->fieldByName('KeyValuePairs[IsImported]'),
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
109
            'addQueryFields() includes KeyValuePairs composite field');
110
        $this->assertNotNull($keyValuePairs->fieldByName('KeyValuePairs[ISBN]'),
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
111
            'addQueryFields() includes KeyValuePairs composite field');
112
        // Reset the type
113
        DMSDocumentSet::$linked_document_type = 'DMSDocument';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'DMSDocument' of type string is incompatible with the declared type object<Varchar> of property $linked_document_type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
114
115
    }
116
117
    public function testAddQueryFieldsIsExtensible(){
118
        DMSDocumentSet::add_extension('StubDocumentSetMockExtension');
119
120
        $fields = new FieldList(new TabSet('Root'));
121
        $set = new DMSDocumentSet;
122
        $set->addQueryFields($fields);
123
124
        $this->assertNotNull($fields->dataFieldByName('ExtendedField'),
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

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...
125
            'addQueryFields() is extendible as it included the field from the extension');
126
    }
127
128
    /**
129
     * Test that extra documents are added after write
130
     */
131
    public function testSaveLinkedDocuments(){
132
        /** @var DMSDocumentSet $set */
133
        $set = $this->objFromFixture('DMSDocumentSet','ds6');
134
135
        // Assert initially docs
136
        $this->assertEquals(1, $set->getDocuments()->count(), 'Set 6 has 1 document');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentSetTest>.

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...
137
138
        // Now apply the query and see if 2 extras were added with CreatedByID filter
139
        $set->KeyValuePairs = '{"CreatedByID":["1"]}';
140
        $set->write();
141
        $this->assertEquals(3, $set->getDocuments()->count(), 'Set 6 has 3 documents');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentSetTest>.

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...
142
    }
143
}
144
145
class DMSDocument_Mock extends DMSDocument implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
146
{
147
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
148
        'IsImported' => 'Boolean',
149
        'ISBN'       => 'Varchar',
150
    );
151
152
    private static $searchable_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $searchable_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
153
        'IsImported',
154
        'ISBN',
155
    );
156
}
157
158
class StubDocumentSetMockExtension extends DataExtension implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
159
{
160
    /**
161
     *
162
     * For method {@link DMSDocumentSet::addQueryFields}
163
     *
164
     * @param FieldList $fields
165
     *
166
     * @return FieldList
167
     */
168
    public function updateQueryFields($fields)
169
    {
170
        $fields->addFieldToTab('Root.QueryBuilder', new TextField('ExtendedField'));
171
172
        return $fields;
173
    }
174
175
}