Completed
Pull Request — 3.4 (#6304)
by Loz
08:29
created

MemberTest::testCurrentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class MemberTest extends FunctionalTest {
7
	protected static $fixture_file = 'MemberTest.yml';
8
9
	protected $orig = array();
10
	protected $local = null;
11
12
	protected $illegalExtensions = array(
13
		'Member' => array(
14
			// TODO Coupling with modules, this should be resolved by automatically
15
			// removing all applied extensions before a unit test
16
			'ForumRole',
17
			'OpenIDAuthenticatedRole'
18
		)
19
	);
20
21
	public function __construct() {
22
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FunctionalTest as the method __construct() does only exist in the following sub-classes of FunctionalTest: MemberTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
23
24
		//Setting the locale has to happen in the constructor (using the setUp and tearDown methods doesn't work)
25
		//This is because the test relies on the yaml file being interpreted according to a particular date format
26
		//and this setup occurs before the setUp method is run
27
		$this->local = i18n::default_locale();
0 ignored issues
show
Deprecated Code introduced by
The method i18n::default_locale() has been deprecated with message: since version 4.0; Use the "i18n.default_locale" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
28
		i18n::set_default_locale('en_US');
0 ignored issues
show
Deprecated Code introduced by
The method i18n::set_default_locale() has been deprecated with message: since version 4.0; Use the "i18n.default_locale" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
29
	}
30
31
	public function __destruct() {
32
		i18n::set_default_locale($this->local);
0 ignored issues
show
Deprecated Code introduced by
The method i18n::set_default_locale() has been deprecated with message: since version 4.0; Use the "i18n.default_locale" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33
	}
34
35
	public function setUp() {
36
		parent::setUp();
37
38
		$this->orig['Member_unique_identifier_field'] = Member::config()->unique_identifier_field;
0 ignored issues
show
Documentation introduced by
The property unique_identifier_field does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
		Member::config()->unique_identifier_field = 'Email';
0 ignored issues
show
Documentation introduced by
The property unique_identifier_field does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
		Member::set_password_validator(null);
41
	}
42
43
	public function tearDown() {
44
		Member::config()->unique_identifier_field = $this->orig['Member_unique_identifier_field'];
0 ignored issues
show
Documentation introduced by
The property unique_identifier_field does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
		parent::tearDown();
46
	}
47
48
49
50
	/**
51
	 * @expectedException ValidationException
52
	 */
53
	public function testWriteDoesntMergeNewRecordWithExistingMember() {
54
		$m1 = new Member();
55
		$m1->Email = '[email protected]';
56
		$m1->write();
57
58
		$m2 = new Member();
59
		$m2->Email = '[email protected]';
60
		$m2->write();
61
	}
62
63
	/**
64
	 * @expectedException ValidationException
65
	 */
66
	public function testWriteDoesntMergeExistingMemberOnIdentifierChange() {
67
		$m1 = new Member();
68
		$m1->Email = '[email protected]';
69
		$m1->write();
70
71
		$m2 = new Member();
72
		$m2->Email = '[email protected]';
73
		$m2->write();
74
75
		$m2->Email = '[email protected]';
76
		$m2->write();
77
	}
78
79
	public function testDefaultPasswordEncryptionOnMember() {
80
		$memberWithPassword = new Member();
81
		$memberWithPassword->Password = 'mypassword';
82
		$memberWithPassword->write();
83
		$this->assertEquals(
84
			$memberWithPassword->PasswordEncryption,
85
			Security::config()->password_encryption_algorithm,
86
			'Password encryption is set for new member records on first write (with setting "Password")'
87
		);
88
89
		$memberNoPassword = new Member();
90
		$memberNoPassword->write();
91
		$this->assertNull(
92
			$memberNoPassword->PasswordEncryption,
93
			'Password encryption is not set for new member records on first write, when not setting a "Password")'
94
		);
95
	}
96
97
	public function testDefaultPasswordEncryptionDoesntChangeExistingMembers() {
98
		$member = new Member();
99
		$member->Password = 'mypassword';
100
		$member->PasswordEncryption = 'sha1_v2.4';
101
		$member->write();
102
103
		$origAlgo = Security::config()->password_encryption_algorithm;
0 ignored issues
show
Documentation introduced by
The property password_encryption_algorithm does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
		Security::config()->password_encryption_algorithm = 'none';
0 ignored issues
show
Documentation introduced by
The property password_encryption_algorithm does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
106
		$member->Password = 'mynewpassword';
107
		$member->write();
108
109
		$this->assertEquals(
110
			$member->PasswordEncryption,
111
			'sha1_v2.4'
112
		);
113
		$result = $member->checkPassword('mynewpassword');
114
		$this->assertTrue($result->valid());
115
116
		Security::config()->password_encryption_algorithm = $origAlgo;
0 ignored issues
show
Documentation introduced by
The property password_encryption_algorithm does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
	}
118
119
	public function testKeepsEncryptionOnEmptyPasswords() {
120
		$member = new Member();
121
		$member->Password = 'mypassword';
122
		$member->PasswordEncryption = 'sha1_v2.4';
123
		$member->write();
124
125
		$member->Password = '';
126
		$member->write();
127
128
		$this->assertEquals(
129
			$member->PasswordEncryption,
130
			'sha1_v2.4'
131
		);
132
		$result = $member->checkPassword('');
133
		$this->assertTrue($result->valid());
134
	}
135
136
	public function testSetPassword() {
137
		$member = $this->objFromFixture('Member', 'test');
138
		$member->Password = "test1";
0 ignored issues
show
Documentation introduced by
The property Password does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
		$member->write();
140
		$result = $member->checkPassword('test1');
141
		$this->assertTrue($result->valid());
142
	}
143
144
	/**
145
	 * Test that password changes are logged properly
146
	 */
147
	public function testPasswordChangeLogging() {
148
		$member = $this->objFromFixture('Member', 'test');
149
		$this->assertNotNull($member);
150
		$member->Password = "test1";
0 ignored issues
show
Documentation introduced by
The property Password does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
		$member->write();
152
153
		$member->Password = "test2";
0 ignored issues
show
Documentation introduced by
The property Password does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
154
		$member->write();
155
156
		$member->Password = "test3";
0 ignored issues
show
Documentation introduced by
The property Password does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
		$member->write();
158
159
		$passwords = DataObject::get("MemberPassword", "\"MemberID\" = $member->ID", "\"Created\" DESC, \"ID\" DESC")
160
			->getIterator();
161
		$this->assertNotNull($passwords);
162
		$passwords->rewind();
163
		$this->assertTrue($passwords->current()->checkPassword('test3'), "Password test3 not found in MemberRecord");
164
165
		$passwords->next();
166
		$this->assertTrue($passwords->current()->checkPassword('test2'), "Password test2 not found in MemberRecord");
167
168
		$passwords->next();
169
		$this->assertTrue($passwords->current()->checkPassword('test1'), "Password test1 not found in MemberRecord");
170
171
		$passwords->next();
172
		$this->assertInstanceOf('DataObject', $passwords->current());
173
		$this->assertTrue($passwords->current()->checkPassword('1nitialPassword'),
174
			"Password 1nitialPassword not found in MemberRecord");
175
176
		//check we don't retain orphaned records when a member is deleted
177
		$member->delete();
178
179
		$passwords = MemberPassword::get()->filter('MemberID', $member->OldID);
180
181
		$this->assertCount(0, $passwords);
182
	}
183
184
	/**
185
	 * Test that changed passwords will send an email
186
	 */
187
	public function testChangedPasswordEmaling() {
188
		Config::inst()->update('Member', 'notify_password_change', true);
189
190
		$this->clearEmails();
191
192
		$member = $this->objFromFixture('Member', 'test');
193
		$this->assertNotNull($member);
194
		$valid = $member->changePassword('32asDF##$$%%');
195
		$this->assertTrue($valid->valid());
196
197
		$this->assertEmailSent('[email protected]', null, 'Your password has been changed',
198
			'/testuser@example\.com/');
199
200
	}
201
202
	/**
203
	 * Test that triggering "forgotPassword" sends an Email with a reset link
204
	 */
205
	public function testForgotPasswordEmaling() {
206
		$this->clearEmails();
207
		$this->autoFollowRedirection = false;
208
209
		$member = $this->objFromFixture('Member', 'test');
210
		$this->assertNotNull($member);
211
212
		// Initiate a password-reset
213
		$response = $this->post('Security/LostPasswordForm', array('Email' => $member->Email));
214
215
		$this->assertEquals($response->getStatusCode(), 302);
216
217
		// We should get redirected to Security/passwordsent
218
		$this->assertContains('Security/passwordsent/[email protected]',
219
			urldecode($response->getHeader('Location')));
220
221
		// Check existance of reset link
222
		$this->assertEmailSent("[email protected]", null, 'Your password reset link',
223
			'/Security\/changepassword\?m='.$member->ID.'&t=[^"]+/');
224
	}
225
226
	/**
227
	 * Test that passwords validate against NZ e-government guidelines
228
	 *  - don't allow the use of the last 6 passwords
229
	 *  - require at least 3 of lowercase, uppercase, digits and punctuation
230
	 *  - at least 7 characters long
231
	 */
232
	public function testValidatePassword() {
233
		$member = $this->objFromFixture('Member', 'test');
234
		$this->assertNotNull($member);
235
236
		Member::set_password_validator(new MemberTest_PasswordValidator());
237
238
		// BAD PASSWORDS
239
240
		$valid = $member->changePassword('shorty');
241
		$this->assertFalse($valid->valid());
242
		$this->assertContains("TOO_SHORT", $valid->codeList());
243
244
		$valid = $member->changePassword('longone');
245
		$this->assertNotContains("TOO_SHORT", $valid->codeList());
246
		$this->assertContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
247
		$this->assertFalse($valid->valid());
248
249
		$valid = $member->changePassword('w1thNumb3rs');
250
		$this->assertNotContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
251
		$this->assertTrue($valid->valid());
252
253
		// Clear out the MemberPassword table to ensure that the system functions properly in that situation
254
		DB::query("DELETE FROM \"MemberPassword\"");
255
256
		// GOOD PASSWORDS
257
258
		$valid = $member->changePassword('withSym###Ls');
259
		$this->assertNotContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
260
		$this->assertTrue($valid->valid());
261
262
		$valid = $member->changePassword('withSym###Ls2');
263
		$this->assertTrue($valid->valid());
264
265
		$valid = $member->changePassword('withSym###Ls3');
266
		$this->assertTrue($valid->valid());
267
268
		$valid = $member->changePassword('withSym###Ls4');
269
		$this->assertTrue($valid->valid());
270
271
		$valid = $member->changePassword('withSym###Ls5');
272
		$this->assertTrue($valid->valid());
273
274
		$valid = $member->changePassword('withSym###Ls6');
275
		$this->assertTrue($valid->valid());
276
277
		$valid = $member->changePassword('withSym###Ls7');
278
		$this->assertTrue($valid->valid());
279
280
		// CAN'T USE PASSWORDS 2-7, but I can use pasword 1
281
282
		$valid = $member->changePassword('withSym###Ls2');
283
		$this->assertFalse($valid->valid());
284
		$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
285
286
		$valid = $member->changePassword('withSym###Ls5');
287
		$this->assertFalse($valid->valid());
288
		$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
289
290
		$valid = $member->changePassword('withSym###Ls7');
291
		$this->assertFalse($valid->valid());
292
		$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
293
294
		$valid = $member->changePassword('withSym###Ls');
295
		$this->assertTrue($valid->valid());
296
297
		// HAVING DONE THAT, PASSWORD 2 is now available from the list
298
299
		$valid = $member->changePassword('withSym###Ls2');
300
		$this->assertTrue($valid->valid());
301
302
		$valid = $member->changePassword('withSym###Ls3');
303
		$this->assertTrue($valid->valid());
304
305
		$valid = $member->changePassword('withSym###Ls4');
306
		$this->assertTrue($valid->valid());
307
308
		Member::set_password_validator(null);
309
	}
310
311
	/**
312
	 * Test that the PasswordExpiry date is set when passwords are changed
313
	 */
314
	public function testPasswordExpirySetting() {
315
		Member::config()->password_expiry_days = 90;
0 ignored issues
show
Documentation introduced by
The property password_expiry_days does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
316
317
		$member = $this->objFromFixture('Member', 'test');
318
		$this->assertNotNull($member);
319
		$valid = $member->changePassword("Xx?1234234");
320
		$this->assertTrue($valid->valid());
321
322
		$expiryDate = date('Y-m-d', time() + 90*86400);
323
		$this->assertEquals($expiryDate, $member->PasswordExpiry);
324
325
		Member::config()->password_expiry_days = null;
0 ignored issues
show
Documentation introduced by
The property password_expiry_days does not exist on object<Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
326
		$valid = $member->changePassword("Xx?1234235");
327
		$this->assertTrue($valid->valid());
328
329
		$this->assertNull($member->PasswordExpiry);
330
	}
331
332
	public function testIsPasswordExpired() {
333
		$member = $this->objFromFixture('Member', 'test');
334
		$this->assertNotNull($member);
335
		$this->assertFalse($member->isPasswordExpired());
336
337
		$member = $this->objFromFixture('Member', 'noexpiry');
338
		$member->PasswordExpiry = null;
0 ignored issues
show
Documentation introduced by
The property PasswordExpiry does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
339
		$this->assertFalse($member->isPasswordExpired());
340
341
		$member = $this->objFromFixture('Member', 'expiredpassword');
342
		$this->assertTrue($member->isPasswordExpired());
343
344
		// Check the boundary conditions
345
		// If PasswordExpiry == today, then it's expired
346
		$member->PasswordExpiry = date('Y-m-d');
0 ignored issues
show
Documentation introduced by
The property PasswordExpiry does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
347
		$this->assertTrue($member->isPasswordExpired());
348
349
		// If PasswordExpiry == tomorrow, then it's not
350
		$member->PasswordExpiry = date('Y-m-d', time() + 86400);
0 ignored issues
show
Documentation introduced by
The property PasswordExpiry does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
351
		$this->assertFalse($member->isPasswordExpired());
352
353
	}
354
355
	public function testMemberWithNoDateFormatFallsbackToGlobalLocaleDefaultFormat() {
356
		Config::inst()->update('i18n', 'date_format', 'yyyy-MM-dd');
357
		Config::inst()->update('i18n', 'time_format', 'H:mm');
358
		$member = $this->objFromFixture('Member', 'noformatmember');
359
		$this->assertEquals('yyyy-MM-dd', $member->DateFormat);
360
		$this->assertEquals('H:mm', $member->TimeFormat);
361
	}
362
363
	public function testInGroups() {
364
		$staffmember = $this->objFromFixture('Member', 'staffmember');
365
		$managementmember = $this->objFromFixture('Member', 'managementmember');
366
		$accountingmember = $this->objFromFixture('Member', 'accountingmember');
367
		$ceomember = $this->objFromFixture('Member', 'ceomember');
368
369
		$staffgroup = $this->objFromFixture('Group', 'staffgroup');
370
		$managementgroup = $this->objFromFixture('Group', 'managementgroup');
371
		$accountinggroup = $this->objFromFixture('Group', 'accountinggroup');
372
		$ceogroup = $this->objFromFixture('Group', 'ceogroup');
373
374
		$this->assertTrue(
375
			$staffmember->inGroups(array($staffgroup, $managementgroup)),
376
			'inGroups() succeeds if a membership is detected on one of many passed groups'
377
		);
378
		$this->assertFalse(
379
			$staffmember->inGroups(array($ceogroup, $managementgroup)),
380
			'inGroups() fails if a membership is detected on none of the passed groups'
381
		);
382
		$this->assertFalse(
383
			$ceomember->inGroups(array($staffgroup, $managementgroup), true),
384
			'inGroups() fails if no direct membership is detected on any of the passed groups (in strict mode)'
385
		);
386
	}
387
388
	public function testAddToGroupByCode() {
389
		$grouplessMember = $this->objFromFixture('Member', 'grouplessmember');
390
		$memberlessGroup = $this->objFromFixture('Group','memberlessgroup');
391
392
		$this->assertFalse($grouplessMember->Groups()->exists());
393
		$this->assertFalse($memberlessGroup->Members()->exists());
394
395
		$grouplessMember->addToGroupByCode('memberless');
396
397
		$this->assertEquals($memberlessGroup->Members()->Count(), 1);
398
		$this->assertEquals($grouplessMember->Groups()->Count(), 1);
399
400
		$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
401
		$this->assertEquals($grouplessMember->Groups()->Count(), 2);
402
403
		$group = DataObject::get_one('Group', array(
404
			'"Group"."Code"' => 'somegroupthatwouldneverexist'
405
		));
406
		$this->assertNotNull($group);
407
		$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
408
		$this->assertEquals($group->Title, 'New Group');
409
410
	}
411
412
	public function testRemoveFromGroupByCode() {
413
		$grouplessMember = $this->objFromFixture('Member', 'grouplessmember');
414
		$memberlessGroup = $this->objFromFixture('Group','memberlessgroup');
415
416
		$this->assertFalse($grouplessMember->Groups()->exists());
417
		$this->assertFalse($memberlessGroup->Members()->exists());
418
419
		$grouplessMember->addToGroupByCode('memberless');
420
421
		$this->assertEquals($memberlessGroup->Members()->Count(), 1);
422
		$this->assertEquals($grouplessMember->Groups()->Count(), 1);
423
424
		$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
425
		$this->assertEquals($grouplessMember->Groups()->Count(), 2);
426
427
		$group = DataObject::get_one('Group', "\"Code\" = 'somegroupthatwouldneverexist'");
428
		$this->assertNotNull($group);
429
		$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
430
		$this->assertEquals($group->Title, 'New Group');
431
432
		$grouplessMember->removeFromGroupByCode('memberless');
433
		$this->assertEquals($memberlessGroup->Members()->Count(), 0);
434
		$this->assertEquals($grouplessMember->Groups()->Count(), 1);
435
436
		$grouplessMember->removeFromGroupByCode('somegroupthatwouldneverexist');
437
		$this->assertEquals($grouplessMember->Groups()->Count(), 0);
438
	}
439
440
	public function testInGroup() {
441
		$staffmember = $this->objFromFixture('Member', 'staffmember');
442
		$managementmember = $this->objFromFixture('Member', 'managementmember');
443
		$accountingmember = $this->objFromFixture('Member', 'accountingmember');
444
		$ceomember = $this->objFromFixture('Member', 'ceomember');
445
446
		$staffgroup = $this->objFromFixture('Group', 'staffgroup');
447
		$managementgroup = $this->objFromFixture('Group', 'managementgroup');
448
		$accountinggroup = $this->objFromFixture('Group', 'accountinggroup');
449
		$ceogroup = $this->objFromFixture('Group', 'ceogroup');
450
451
		$this->assertTrue(
452
			$staffmember->inGroup($staffgroup),
453
			'Direct group membership is detected'
454
		);
455
		$this->assertTrue(
456
			$managementmember->inGroup($staffgroup),
457
			'Users of child group are members of a direct parent group (if not in strict mode)'
458
		);
459
		$this->assertTrue(
460
			$accountingmember->inGroup($staffgroup),
461
			'Users of child group are members of a direct parent group (if not in strict mode)'
462
		);
463
		$this->assertTrue(
464
			$ceomember->inGroup($staffgroup),
465
			'Users of indirect grandchild group are members of a parent group (if not in strict mode)'
466
		);
467
		$this->assertTrue(
468
			$ceomember->inGroup($ceogroup, true),
469
			'Direct group membership is dected (if in strict mode)'
470
		);
471
		$this->assertFalse(
472
			$ceomember->inGroup($staffgroup, true),
473
			'Users of child group are not members of a direct parent group (if in strict mode)'
474
		);
475
		$this->assertFalse(
476
			$staffmember->inGroup($managementgroup),
477
			'Users of parent group are not members of a direct child group'
478
		);
479
		$this->assertFalse(
480
			$staffmember->inGroup($ceogroup),
481
			'Users of parent group are not members of an indirect grandchild group'
482
		);
483
		$this->assertFalse(
484
			$accountingmember->inGroup($managementgroup),
485
			'Users of group are not members of any siblings'
486
		);
487
		$this->assertFalse(
488
			$staffmember->inGroup('does-not-exist'),
489
			'Non-existant group returns false'
490
		);
491
	}
492
493
	/**
494
	 * Tests that the user is able to view their own record, and in turn, they can
495
	 * edit and delete their own record too.
496
	 */
497
	public function testCanManipulateOwnRecord() {
498
		$extensions = $this->removeExtensions(Object::get_extensions('Member'));
499
		$member = $this->objFromFixture('Member', 'test');
500
		$member2 = $this->objFromFixture('Member', 'staffmember');
501
502
		$this->session()->inst_set('loggedInAs', null);
503
504
		/* Not logged in, you can't view, delete or edit the record */
505
		$this->assertFalse($member->canView());
506
		$this->assertFalse($member->canDelete());
507
		$this->assertFalse($member->canEdit());
508
509
		/* Logged in users can edit their own record */
510
		$this->session()->inst_set('loggedInAs', $member->ID);
511
		$this->assertTrue($member->canView());
512
		$this->assertFalse($member->canDelete());
513
		$this->assertTrue($member->canEdit());
514
515
		/* Other uses cannot view, delete or edit others records */
516
		$this->session()->inst_set('loggedInAs', $member2->ID);
517
		$this->assertFalse($member->canView());
518
		$this->assertFalse($member->canDelete());
519
		$this->assertFalse($member->canEdit());
520
521
		$this->addExtensions($extensions);
522
		$this->session()->inst_set('loggedInAs', null);
523
	}
524
525
	public function testAuthorisedMembersCanManipulateOthersRecords() {
526
		$extensions = $this->removeExtensions(Object::get_extensions('Member'));
527
		$member = $this->objFromFixture('Member', 'test');
528
		$member2 = $this->objFromFixture('Member', 'staffmember');
529
530
		/* Group members with SecurityAdmin permissions can manipulate other records */
531
		$this->session()->inst_set('loggedInAs', $member->ID);
532
		$this->assertTrue($member2->canView());
533
		$this->assertTrue($member2->canDelete());
534
		$this->assertTrue($member2->canEdit());
535
536
		$this->addExtensions($extensions);
537
		$this->session()->inst_set('loggedInAs', null);
538
	}
539
540
	public function testExtendedCan() {
541
		$extensions = $this->removeExtensions(Object::get_extensions('Member'));
542
		$member = $this->objFromFixture('Member', 'test');
543
544
		/* Normal behaviour is that you can't view a member unless canView() on an extension returns true */
545
		$this->assertFalse($member->canView());
546
		$this->assertFalse($member->canDelete());
547
		$this->assertFalse($member->canEdit());
548
549
		/* Apply a extension that allows viewing in any case (most likely the case for member profiles) */
550
		Member::add_extension('MemberTest_ViewingAllowedExtension');
551
		$member2 = $this->objFromFixture('Member', 'staffmember');
552
553
		$this->assertTrue($member2->canView());
554
		$this->assertFalse($member2->canDelete());
555
		$this->assertFalse($member2->canEdit());
556
557
		/* Apply a extension that denies viewing of the Member */
558
		Member::remove_extension('MemberTest_ViewingAllowedExtension');
559
		Member::add_extension('MemberTest_ViewingDeniedExtension');
560
		$member3 = $this->objFromFixture('Member', 'managementmember');
561
562
		$this->assertFalse($member3->canView());
563
		$this->assertFalse($member3->canDelete());
564
		$this->assertFalse($member3->canEdit());
565
566
		/* Apply a extension that allows viewing and editing but denies deletion */
567
		Member::remove_extension('MemberTest_ViewingDeniedExtension');
568
		Member::add_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
569
		$member4 = $this->objFromFixture('Member', 'accountingmember');
570
571
		$this->assertTrue($member4->canView());
572
		$this->assertFalse($member4->canDelete());
573
		$this->assertTrue($member4->canEdit());
574
575
		Member::remove_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
576
		$this->addExtensions($extensions);
577
	}
578
579
	/**
580
	 * Tests for {@link Member::getName()} and {@link Member::setName()}
581
	 */
582
	public function testName() {
583
		$member = $this->objFromFixture('Member', 'test');
584
		$member->setName('Test Some User');
585
		$this->assertEquals('Test Some User', $member->getName());
586
		$member->setName('Test');
587
		$this->assertEquals('Test', $member->getName());
588
		$member->FirstName = 'Test';
0 ignored issues
show
Documentation introduced by
The property FirstName does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
589
		$member->Surname = '';
0 ignored issues
show
Documentation introduced by
The property Surname does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
590
		$this->assertEquals('Test', $member->getName());
591
	}
592
593
	public function testMembersWithSecurityAdminAccessCantEditAdminsUnlessTheyreAdminsThemselves() {
594
		$adminMember = $this->objFromFixture('Member', 'admin');
595
		$otherAdminMember = $this->objFromFixture('Member', 'other-admin');
596
		$securityAdminMember = $this->objFromFixture('Member', 'test');
597
		$ceoMember = $this->objFromFixture('Member', 'ceomember');
598
599
		// Careful: Don't read as english language.
600
		// More precisely this should read canBeEditedBy()
601
602
		$this->assertTrue($adminMember->canEdit($adminMember), 'Admins can edit themselves');
0 ignored issues
show
Bug introduced by
It seems like $adminMember defined by $this->objFromFixture('Member', 'admin') on line 594 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
603
		$this->assertTrue($otherAdminMember->canEdit($adminMember), 'Admins can edit other admins');
0 ignored issues
show
Bug introduced by
It seems like $adminMember defined by $this->objFromFixture('Member', 'admin') on line 594 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
604
		$this->assertTrue($securityAdminMember->canEdit($adminMember), 'Admins can edit other members');
0 ignored issues
show
Bug introduced by
It seems like $adminMember defined by $this->objFromFixture('Member', 'admin') on line 594 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
605
606
		$this->assertTrue($securityAdminMember->canEdit($securityAdminMember), 'Security-Admins can edit themselves');
0 ignored issues
show
Bug introduced by
It seems like $securityAdminMember defined by $this->objFromFixture('Member', 'test') on line 596 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
607
		$this->assertFalse($adminMember->canEdit($securityAdminMember), 'Security-Admins can not edit other admins');
0 ignored issues
show
Bug introduced by
It seems like $securityAdminMember defined by $this->objFromFixture('Member', 'test') on line 596 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
608
		$this->assertTrue($ceoMember->canEdit($securityAdminMember), 'Security-Admins can edit other members');
0 ignored issues
show
Bug introduced by
It seems like $securityAdminMember defined by $this->objFromFixture('Member', 'test') on line 596 can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
609
	}
610
611
	public function testOnChangeGroups() {
612
		$staffGroup = $this->objFromFixture('Group', 'staffgroup');
613
		$staffMember = $this->objFromFixture('Member', 'staffmember');
614
		$adminMember = $this->objFromFixture('Member', 'admin');
615
		$newAdminGroup = new Group(array('Title' => 'newadmin'));
616
		$newAdminGroup->write();
617
		Permission::grant($newAdminGroup->ID, 'ADMIN');
618
		$newOtherGroup = new Group(array('Title' => 'othergroup'));
619
		$newOtherGroup->write();
620
621
		$this->assertTrue(
622
			$staffMember->onChangeGroups(array($staffGroup->ID)),
623
			'Adding existing non-admin group relation is allowed for non-admin members'
624
		);
625
		$this->assertTrue(
626
			$staffMember->onChangeGroups(array($newOtherGroup->ID)),
627
			'Adding new non-admin group relation is allowed for non-admin members'
628
		);
629
		$this->assertFalse(
630
			$staffMember->onChangeGroups(array($newAdminGroup->ID)),
631
			'Adding new admin group relation is not allowed for non-admin members'
632
		);
633
634
		$this->session()->inst_set('loggedInAs', $adminMember->ID);
635
		$this->assertTrue(
636
			$staffMember->onChangeGroups(array($newAdminGroup->ID)),
637
			'Adding new admin group relation is allowed for normal users, when granter is logged in as admin'
638
		);
639
		$this->session()->inst_set('loggedInAs', null);
640
641
		$this->assertTrue(
642
			$adminMember->onChangeGroups(array($newAdminGroup->ID)),
643
			'Adding new admin group relation is allowed for admin members'
644
		);
645
	}
646
647
	/**
648
	 * Test Member_GroupSet::add
649
	 */
650
	public function testOnChangeGroupsByAdd() {
651
		$staffMember = $this->objFromFixture('Member', 'staffmember');
652
		$adminMember = $this->objFromFixture('Member', 'admin');
653
654
		// Setup new admin group
655
		$newAdminGroup = new Group(array('Title' => 'newadmin'));
656
		$newAdminGroup->write();
657
		Permission::grant($newAdminGroup->ID, 'ADMIN');
658
659
		// Setup non-admin group
660
		$newOtherGroup = new Group(array('Title' => 'othergroup'));
661
		$newOtherGroup->write();
662
663
		// Test staff can be added to other group
664
		$this->assertFalse($staffMember->inGroup($newOtherGroup));
665
		$staffMember->Groups()->add($newOtherGroup);
666
		$this->assertTrue(
667
			$staffMember->inGroup($newOtherGroup),
668
			'Adding new non-admin group relation is allowed for non-admin members'
669
		);
670
671
		// Test staff member can't be added to admin groups
672
		$this->assertFalse($staffMember->inGroup($newAdminGroup));
673
		$staffMember->Groups()->add($newAdminGroup);
674
		$this->assertFalse(
675
			$staffMember->inGroup($newAdminGroup),
676
			'Adding new admin group relation is not allowed for non-admin members'
677
		);
678
679
		// Test staff member can be added to admin group by admins
680
		$this->logInAs($adminMember);
681
		$staffMember->Groups()->add($newAdminGroup);
682
		$this->assertTrue(
683
			$staffMember->inGroup($newAdminGroup),
684
			'Adding new admin group relation is allowed for normal users, when granter is logged in as admin'
685
		);
686
687
		// Test staff member can be added if they are already admin
688
		$this->session()->inst_set('loggedInAs', null);
689
		$this->assertFalse($adminMember->inGroup($newAdminGroup));
690
		$adminMember->Groups()->add($newAdminGroup);
691
		$this->assertTrue(
692
			$adminMember->inGroup($newAdminGroup),
693
			'Adding new admin group relation is allowed for admin members'
694
		);
695
	}
696
697
	/**
698
	 * Test Member_GroupSet::add
699
	 */
700
	public function testOnChangeGroupsBySetIDList() {
701
		$staffMember = $this->objFromFixture('Member', 'staffmember');
702
703
		// Setup new admin group
704
		$newAdminGroup = new Group(array('Title' => 'newadmin'));
705
		$newAdminGroup->write();
706
		Permission::grant($newAdminGroup->ID, 'ADMIN');
707
708
		// Test staff member can't be added to admin groups
709
		$this->assertFalse($staffMember->inGroup($newAdminGroup));
710
		$staffMember->Groups()->setByIDList(array($newAdminGroup->ID));
711
		$this->assertFalse(
712
			$staffMember->inGroup($newAdminGroup),
713
			'Adding new admin group relation is not allowed for non-admin members'
714
		);
715
	}
716
717
	/**
718
	 * Test that extensions using updateCMSFields() are applied correctly
719
	 */
720
	public function testUpdateCMSFields() {
721
		Member::add_extension('MemberTest_FieldsExtension');
722
723
		$member = singleton('Member');
724
		$fields = $member->getCMSFields();
725
726
		$this->assertNotNull($fields->dataFieldByName('Email'), 'Scaffolded fields are retained');
727
		$this->assertNull($fields->dataFieldByName('Salt'), 'Field modifications run correctly');
728
		$this->assertNotNull($fields->dataFieldByName('TestMemberField'), 'Extension is applied correctly');
729
730
		Member::remove_extension('MemberTest_FieldsExtension');
731
	}
732
733
	/**
734
	 * Test that all members are returned
735
	 */
736
	public function testMap_in_groupsReturnsAll() {
737
		$members = Member::map_in_groups();
738
		$this->assertEquals(13, count($members), 'There are 12 members in the mock plus a fake admin');
739
	}
740
741
	/**
742
	 * Test that only admin members are returned
743
	 */
744
	public function testMap_in_groupsReturnsAdmins() {
745
		$adminID = $this->objFromFixture('Group', 'admingroup')->ID;
746
		$members = Member::map_in_groups($adminID);
747
748
		$admin = $this->objFromFixture('Member', 'admin');
749
		$otherAdmin = $this->objFromFixture('Member', 'other-admin');
750
751
		$this->assertTrue(in_array($admin->getTitle(), $members),
752
			$admin->getTitle().' should be in the returned list.');
753
		$this->assertTrue(in_array($otherAdmin->getTitle(), $members),
754
			$otherAdmin->getTitle().' should be in the returned list.');
755
		$this->assertEquals(2, count($members), 'There should be 2 members from the admin group');
756
	}
757
758
	/**
759
	 * Add the given array of member extensions as class names.
760
	 * This is useful for re-adding extensions after being removed
761
	 * in a test case to produce an unbiased test.
762
	 *
763
	 * @param array $extensions
764
	 * @return array The added extensions
765
	 */
766
	protected function addExtensions($extensions) {
767
		if($extensions) foreach($extensions as $extension) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extensions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
768
			Member::add_extension($extension);
769
		}
770
		return $extensions;
771
	}
772
773
	/**
774
	 * Remove given extensions from Member. This is useful for
775
	 * removing extensions that could produce a biased
776
	 * test result, as some extensions applied by project
777
	 * code or modules can do this.
778
	 *
779
	 * @param array $extensions
780
	 * @return array The removed extensions
781
	 */
782
	protected function removeExtensions($extensions) {
783
		if($extensions) foreach($extensions as $extension) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extensions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
784
			Member::remove_extension($extension);
785
		}
786
		return $extensions;
787
	}
788
789
	public function testGenerateAutologinTokenAndStoreHash() {
790
		$enc = new PasswordEncryptor_Blowfish();
791
792
		$m = new Member();
793
		$m->PasswordEncryption = 'blowfish';
794
		$m->Salt = $enc->salt('123');
795
796
		$token = $m->generateAutologinTokenAndStoreHash();
797
798
		$this->assertEquals($m->encryptWithUserSettings($token), $m->AutoLoginHash, 'Stores the token as ahash.');
799
	}
800
801
	public function testValidateAutoLoginToken() {
802
		$enc = new PasswordEncryptor_Blowfish();
803
804
		$m1 = new Member();
805
		$m1->PasswordEncryption = 'blowfish';
806
		$m1->Salt = $enc->salt('123');
807
		$m1Token = $m1->generateAutologinTokenAndStoreHash();
808
809
		$m2 = new Member();
810
		$m2->PasswordEncryption = 'blowfish';
811
		$m2->Salt = $enc->salt('456');
812
		$m2Token = $m2->generateAutologinTokenAndStoreHash();
813
814
		$this->assertTrue($m1->validateAutoLoginToken($m1Token), 'Passes token validity test against matching member.');
815
		$this->assertFalse($m2->validateAutoLoginToken($m1Token), 'Fails token validity test against other member.');
816
	}
817
818
	public function testCanDelete() {
819
		$admin1 = $this->objFromFixture('Member', 'admin');
820
		$admin2 = $this->objFromFixture('Member', 'other-admin');
821
		$member1 = $this->objFromFixture('Member', 'grouplessmember');
822
		$member2 = $this->objFromFixture('Member', 'noformatmember');
823
824
		$this->assertTrue(
825
			$admin1->canDelete($admin2),
0 ignored issues
show
Bug introduced by
It seems like $admin2 defined by $this->objFromFixture('Member', 'other-admin') on line 820 can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
826
			'Admins can delete other admins'
827
		);
828
		$this->assertTrue(
829
			$member1->canDelete($admin2),
0 ignored issues
show
Bug introduced by
It seems like $admin2 defined by $this->objFromFixture('Member', 'other-admin') on line 820 can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
830
			'Admins can delete non-admins'
831
		);
832
		$this->assertFalse(
833
			$admin1->canDelete($admin1),
0 ignored issues
show
Bug introduced by
It seems like $admin1 defined by $this->objFromFixture('Member', 'admin') on line 819 can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
834
			'Admins can not delete themselves'
835
		);
836
		$this->assertFalse(
837
			$member1->canDelete($member2),
0 ignored issues
show
Bug introduced by
It seems like $member2 defined by $this->objFromFixture('Member', 'noformatmember') on line 822 can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
838
			'Non-admins can not delete other non-admins'
839
		);
840
		$this->assertFalse(
841
			$member1->canDelete($member1),
0 ignored issues
show
Bug introduced by
It seems like $member1 defined by $this->objFromFixture('M...er', 'grouplessmember') on line 821 can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
842
			'Non-admins can not delete themselves'
843
		);
844
	}
845
846
	public function testFailedLoginCount() {
847
		$maxFailedLoginsAllowed = 3;
848
		//set up the config variables to enable login lockouts
849
		Config::nest();
850
		Config::inst()->update('Member', 'lock_out_after_incorrect_logins', $maxFailedLoginsAllowed);
851
852
		$member = $this->objFromFixture('Member', 'test');
853
		$failedLoginCount = $member->FailedLoginCount;
0 ignored issues
show
Documentation introduced by
The property FailedLoginCount does not exist on object<DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
854
855
		for ($i = 1; $i < $maxFailedLoginsAllowed; ++$i) {
856
			$member->registerFailedLogin();
857
858
			$this->assertEquals(
859
				++$failedLoginCount,
860
				$member->FailedLoginCount,
861
				'Failed to increment $member->FailedLoginCount'
862
			);
863
864
			$this->assertFalse(
865
				$member->isLockedOut(),
866
				"Member has been locked out too early"
867
			);
868
		}
869
	}
870
871
	public function testMemberValidator()
872
	{
873
		// clear custom requirements for this test
874
		Config::inst()->update('Member_Validator', 'customRequired', null);
875
		$memberA = $this->objFromFixture('Member', 'admin');
876
		$memberB = $this->objFromFixture('Member', 'test');
877
878
		// create a blank form
879
		$form = new MemberTest_ValidatorForm();
880
881
		$validator = new Member_Validator();
882
		$validator->setForm($form);
883
884
		// Simulate creation of a new member via form, but use an existing member identifier
885
		$fail = $validator->php(array(
886
			'FirstName' => 'Test',
887
			'Email' => $memberA->Email
888
		));
889
890
		$this->assertFalse(
891
			$fail,
892
			'Member_Validator must fail when trying to create new Member with existing Email.'
893
		);
894
895
		// populate the form with values from another member
896
		$form->loadDataFrom($memberB);
0 ignored issues
show
Bug introduced by
It seems like $memberB defined by $this->objFromFixture('Member', 'test') on line 876 can be null; however, Form::loadDataFrom() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
897
898
		// Assign the validator to an existing member
899
		// (this is basically the same as passing the member ID with the form data)
900
		$validator->setForMember($memberB);
901
902
		// Simulate update of a member via form and use an existing member Email
903
		$fail = $validator->php(array(
904
			'FirstName' => 'Test',
905
			'Email' => $memberA->Email
906
		));
907
908
		// Simulate update to a new Email address
909
		$pass1 = $validator->php(array(
910
			'FirstName' => 'Test',
911
			'Email' => '[email protected]'
912
		));
913
914
		// Pass in the same Email address that the member already has. Ensure that case is valid
915
		$pass2 = $validator->php(array(
916
			'FirstName' => 'Test',
917
			'Surname' => 'User',
918
			'Email' => $memberB->Email
919
		));
920
921
		$this->assertFalse(
922
			$fail,
923
			'Member_Validator must fail when trying to update existing member with existing Email.'
924
		);
925
926
		$this->assertTrue(
927
			$pass1,
928
			'Member_Validator must pass when Email is updated to a value that\'s not in use.'
929
		);
930
931
		$this->assertTrue(
932
			$pass2,
933
			'Member_Validator must pass when Member updates his own Email to the already existing value.'
934
		);
935
	}
936
937
	public function testMemberValidatorWithExtensions()
938
	{
939
		// clear custom requirements for this test
940
		Config::inst()->update('Member_Validator', 'customRequired', null);
941
942
		// create a blank form
943
		$form = new MemberTest_ValidatorForm();
944
945
		// Test extensions
946
		Member_Validator::add_extension('MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension');
947
		$validator = new Member_Validator();
948
		$validator->setForm($form);
949
950
		// This test should fail, since the extension enforces FirstName == Surname
951
		$fail = $validator->php(array(
952
			'FirstName' => 'Test',
953
			'Surname' => 'User',
954
			'Email' => '[email protected]'
955
		));
956
957
		$pass = $validator->php(array(
958
			'FirstName' => 'Test',
959
			'Surname' => 'Test',
960
			'Email' => '[email protected]'
961
		));
962
963
		$this->assertFalse(
964
			$fail,
965
			'Member_Validator must fail because of added extension.'
966
		);
967
968
		$this->assertTrue(
969
			$pass,
970
			'Member_Validator must succeed, since it meets all requirements.'
971
		);
972
973
		// Add another extension that always fails. This ensures that all extensions are considered in the validation
974
		Member_Validator::add_extension('MemberTest_MemberValidator_AlwaysFailsExtension');
975
		$validator = new Member_Validator();
976
		$validator->setForm($form);
977
978
		// Even though the data is valid, This test should still fail, since one extension always returns false
979
		$fail = $validator->php(array(
980
			'FirstName' => 'Test',
981
			'Surname' => 'Test',
982
			'Email' => '[email protected]'
983
		));
984
985
		$this->assertFalse(
986
			$fail,
987
			'Member_Validator must fail because of added extensions.'
988
		);
989
990
		// Remove added extensions
991
		Member_Validator::remove_extension('MemberTest_MemberValidator_AlwaysFailsExtension');
992
		Member_Validator::remove_extension('MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension');
993
	}
994
995
	public function testCustomMemberValidator()
996
	{
997
		// clear custom requirements for this test
998
		Config::inst()->update('Member_Validator', 'customRequired', null);
999
1000
		$member = $this->objFromFixture('Member', 'admin');
1001
1002
		$form = new MemberTest_ValidatorForm();
1003
		$form->loadDataFrom($member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->objFromFixture('Member', 'admin') on line 1000 can be null; however, Form::loadDataFrom() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1004
1005
		$validator = new Member_Validator();
1006
		$validator->setForm($form);
1007
1008
		$pass = $validator->php(array(
1009
			'FirstName' => 'Borris',
1010
			'Email' => '[email protected]'
1011
		));
1012
1013
		$fail = $validator->php(array(
1014
			'Email' => '[email protected]',
1015
			'Surname' => ''
1016
		));
1017
1018
		$this->assertTrue($pass, 'Validator requires a FirstName and Email');
1019
		$this->assertFalse($fail, 'Missing FirstName');
1020
1021
		$ext = new MemberTest_ValidatorExtension();
1022
		$ext->updateValidator($validator);
1023
1024
		$pass = $validator->php(array(
1025
			'FirstName' => 'Borris',
1026
			'Email' => '[email protected]'
1027
		));
1028
1029
		$fail = $validator->php(array(
1030
			'Email' => '[email protected]'
1031
		));
1032
1033
		$this->assertFalse($pass, 'Missing surname');
1034
		$this->assertFalse($fail, 'Missing surname value');
1035
1036
		$fail = $validator->php(array(
1037
			'Email' => '[email protected]',
1038
			'Surname' => 'Silverman'
1039
		));
1040
1041
		$this->assertTrue($fail, 'Passes with email and surname now (no firstname)');
1042
	}
1043
1044
	public function testCurrentUser() {
1045
		$this->assertNull(Member::currentUser());
1046
1047
		$adminMember = $this->objFromFixture('Member', 'admin');
1048
		$this->logInAs($adminMember);
1049
1050
		$userFromSession = Member::currentUser();
1051
		$this->assertEquals($adminMember->ID, $userFromSession->ID);
1052
	}
1053
1054
}
1055
1056
/**
1057
 * @package framework
1058
 * @subpackage tests
1059
 */
1060
class MemberTest_ValidatorForm extends Form implements TestOnly {
1061
1062
	public function __construct() {
1063
		parent::__construct(Controller::curr(), __CLASS__, new FieldList(
1064
			new TextField('Email'),
1065
			new TextField('Surname'),
1066
			new TextField('ID'),
1067
			new TextField('FirstName')
1068
		), new FieldList(
1069
			new FormAction('someAction')
1070
		));
1071
	}
1072
}
1073
1074
/**
1075
 * @package framework
1076
 * @subpackage tests
1077
 */
1078
class MemberTest_ValidatorExtension extends DataExtension implements TestOnly {
1079
1080
	public function updateValidator(&$validator) {
1081
		$validator->addRequiredField('Surname');
1082
		$validator->removeRequiredField('FirstName');
1083
	}
1084
}
1085
1086
/**
1087
 * Extension that adds additional validation criteria
1088
 * @package framework
1089
 * @subpackage tests
1090
 */
1091
class MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension extends DataExtension implements TestOnly
1092
{
1093
	public function updatePHP($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1094
		return $data['FirstName'] == $data['Surname'];
1095
	}
1096
}
1097
1098
/**
1099
 * Extension that adds additional validation criteria
1100
 * @package framework
1101
 * @subpackage tests
1102
 */
1103
class MemberTest_MemberValidator_AlwaysFailsExtension extends DataExtension implements TestOnly
1104
{
1105
	public function updatePHP($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1106
		return false;
1107
	}
1108
}
1109
1110
/**
1111
 * @package framework
1112
 * @subpackage tests
1113
 */
1114
class MemberTest_ViewingAllowedExtension extends DataExtension implements TestOnly {
1115
1116
	public function canView($member = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1117
		return true;
1118
	}
1119
}
1120
1121
/**
1122
 * @package framework
1123
 * @subpackage tests
1124
 */
1125
class MemberTest_ViewingDeniedExtension extends DataExtension implements TestOnly {
1126
1127
	public function canView($member = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1128
		return false;
1129
	}
1130
}
1131
1132
/**
1133
 * @package framework
1134
 * @subpackage tests
1135
 */
1136
class MemberTest_FieldsExtension extends DataExtension implements TestOnly {
1137
1138
	public function updateCMSFields(FieldList $fields) {
1139
		$fields->addFieldToTab('Root.Main', new TextField('TestMemberField', 'Test'));
1140
	}
1141
1142
}
1143
1144
/**
1145
 * @package framework
1146
 * @subpackage tests
1147
 */
1148
class MemberTest_EditingAllowedDeletingDeniedExtension extends DataExtension implements TestOnly {
1149
1150
	public function canView($member = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1151
		return true;
1152
	}
1153
1154
	public function canEdit($member = null) {
1155
		return true;
1156
	}
1157
1158
	public function canDelete($member = null) {
1159
		return false;
1160
	}
1161
1162
}
1163
1164
/**
1165
 * @package framework
1166
 * @subpackage tests
1167
 */
1168
class MemberTest_PasswordValidator extends PasswordValidator {
1169
	public function __construct() {
1170
		parent::__construct();
1171
		$this->minLength(7);
1172
		$this->checkHistoricalPasswords(6);
1173
		$this->characterStrength(3, array('lowercase','uppercase','digits','punctuation'));
1174
	}
1175
1176
}
1177