SoapModelAccessTest::testAuthenticatedPUT()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 50
rs 9.3333
cc 1
eloc 33
nc 1
nop 0
1
<?php
2
/**
3
 * @todo Test Relation getters
4
 * @todo Test filter and limit through GET params
5
 * @todo Test DELETE verb
6
 */
7
class SoapModelAccessTest 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...
8
{
9
    public static $fixture_file = 'SoapModelAccessTest.yml';
10
11
    protected $extraDataObjects = array(
12
        'SoapModelAccessTest_Comment',
13
        'SoapModelAccessTest_Page',
14
    );
15
16
    public function getTestSoapConnection()
17
    {
18
        // We can't actually test the SOAP server itself because there's not currently a way of putting it into "test mode"
19
        return new SOAPModelAccess();
20
21
        // One day, we should build this facility and then return something more like the item below:
22
        // return new SoapClient(Director::absoluteBaseURL() . 'soap/v1/wsdl');
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
    }
24
25
    public function testApiAccess()
26
    {
27
        $c = $this->getTestSoapConnection();
28
        $soapResponse = $c->getXML(
29
            'SoapModelAccessTest_Comment',
30
            1,
31
            null,
32
            null,
33
            '[email protected]',
34
            'editor'
0 ignored issues
show
Unused Code introduced by
The call to SOAPModelAccess::getXML() has too many arguments starting with 'editor'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
35
        );
36
37
        $responseArr = Convert::xml2array($soapResponse);
38
        $this->assertEquals($responseArr['ID'], 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
39
        $this->assertEquals($responseArr['Name'], 'Joe');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
40
    }
41
42
    public function testAuthenticatedPUT()
43
    {
44
        $comment1 = $this->objFromFixture('SoapModelAccessTest_Comment', 'comment1');
45
        $comment1ID = $comment1->ID;
46
47
        // test wrong details
48
        $c = $this->getTestSoapConnection();
49
50
        $updateXML = <<<XML
51
<?xml version="1.0" encoding="UTF-8"?>
52
		<SoapModelAccessTest_Comment>
53
			<ID>$comment1ID</ID>
54
			<Name>Jimmy</Name>
55
		</SoapModelAccessTest_Comment>			
56
XML;
57
58
        $soapResponse = $c->putXML(
59
            'SoapModelAccessTest_Comment',
60
            $comment1->ID,
61
            null,
62
            $updateXML,
63
            '[email protected]',
64
            'wrongpassword'
65
        );
66
        $this->assertEquals('<error type="authentication" code="401">Unauthorized</error>', $soapResponse);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
67
68
        // Check that the details weren't saved
69
        $c = $this->getTestSoapConnection();
70
        $soapResponse = $c->getXML('SoapModelAccessTest_Comment', $comment1->ID, null, '[email protected]', 'editor');
71
        $responseArr = Convert::xml2array($soapResponse);
72
        $this->assertEquals($comment1->ID, $responseArr['ID']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
73
        $this->assertEquals('Joe', $responseArr['Name']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
74
75
        // Now do an update with the right password
76
        $soapResponse = $c->putXML(
0 ignored issues
show
Unused Code introduced by
$soapResponse 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...
77
            'SoapModelAccessTest_Comment',
78
            $comment1->ID,
79
            null,
80
            $updateXML,
81
            '[email protected]',
82
            'editor'
83
        );
84
85
        // Check that the details were saved
86
        $c = $this->getTestSoapConnection();
87
        $soapResponse = $c->getXML('SoapModelAccessTest_Comment', $comment1->ID, null, '[email protected]', 'editor');
88
        $responseArr = Convert::xml2array($soapResponse);
89
        $this->assertEquals($comment1->ID, $responseArr['ID']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
90
        $this->assertEquals('Jimmy', $responseArr['Name']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SoapModelAccessTest>.

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...
91
    }
92
93
    public function testAuthenticatedPOST()
94
    {
95
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
        $c = $this->getTestSoapConnection();
97
        $soapResponse = $c->getXML(
98
            "SoapModelAccessTest_Comment", 
99
            null,
100
            null,
101
            '[email protected]',
102
            'editor'
103
        );
104
        Debug::message($soapResponse);
105
        $responseArr = Convert::xml2array($soapResponse);
106
        Debug::show($responseArr);
107
        $this->assertEquals($responseArr['Name'], 'Created Name');
108
        */
109
    }
110
}
111
112
/**
113
 * Everybody can view comments, logged in members in the "users" group can create comments,
114
 * but only "editors" can edit or delete them.
115
 */
116
class SoapModelAccessTest_Comment extends DataObject implements PermissionProvider,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...
Coding Style introduced by
Expected 1 space before "TestOnly"; 0 found
Loading history...
117
{
118
    public static $api_access = true;
119
120
    public static $db = array(
121
        'Name' => 'Varchar(255)',
122
        'Comment' => 'Text',
123
    );
124
125
    public static $has_many = array();
126
127
    public function providePermissions()
128
    {
129
        return array(
130
            'EDIT_Comment' => 'Edit Comment Objects',
131
            'CREATE_Comment' => 'Create Comment Objects',
132
            'DELETE_Comment' => 'Delete Comment Objects',
133
        );
134
    }
135
136
    public function canView($member = null)
137
    {
138
        return true;
139
    }
140
141
    public function canEdit($member = null)
142
    {
143
        return Permission::checkMember($member, 'EDIT_Comment');
144
    }
145
146
    public function canDelete($member = null)
147
    {
148
        return Permission::checkMember($member, 'DELETE_Comment');
149
    }
150
151
    public function canCreate($member = null)
152
    {
153
        return Permission::checkMember($member, 'CREATE_Comment');
154
    }
155
}
156
157
class SoapModelAccessTest_Page extends DataObject 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...
158
{
159
    public static $api_access = false;
160
161
    public static $db = array(
162
        'Title' => 'Text',
163
        'Content' => 'HTMLText',
164
    );
165
}
166