These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SMW\Tests\Protection; |
||
4 | |||
5 | use SMW\Protection\EditProtectionValidator; |
||
6 | use SMW\DataItemFactory; |
||
7 | |||
8 | /** |
||
9 | * @covers \SMW\Protection\EditProtectionValidator |
||
10 | * @group semantic-mediawiki |
||
11 | * |
||
12 | * @license GNU GPL v2+ |
||
13 | * @since 2.5 |
||
14 | * |
||
15 | * @author mwjames |
||
16 | */ |
||
17 | class EditProtectionValidatorTest extends \PHPUnit_Framework_TestCase { |
||
18 | |||
19 | private $dataItemFactory; |
||
20 | private $cachedPropertyValuesPrefetcher; |
||
21 | private $cache; |
||
22 | |||
23 | protected function setUp() { |
||
24 | parent::setUp(); |
||
25 | |||
26 | $this->dataItemFactory = new DataItemFactory(); |
||
27 | |||
28 | $this->cachedPropertyValuesPrefetcher = $this->getMockBuilder( '\SMW\CachedPropertyValuesPrefetcher' ) |
||
29 | ->disableOriginalConstructor() |
||
30 | ->getMock(); |
||
31 | |||
32 | $this->cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
||
33 | ->disableOriginalConstructor() |
||
34 | ->getMock(); |
||
35 | } |
||
36 | |||
37 | public function testCanConstruct() { |
||
38 | |||
39 | $this->assertInstanceOf( |
||
40 | EditProtectionValidator::class, |
||
41 | new EditProtectionValidator( $this->cachedPropertyValuesPrefetcher, $this->cache ) |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | public function testHasProtectionOnNamespace() { |
||
46 | |||
47 | $subject = $this->dataItemFactory->newDIWikiPage( 'Foo', NS_MAIN, '', 'Bar' ); |
||
48 | $property = $this->dataItemFactory->newDIProperty( '_EDIP' ); |
||
49 | |||
50 | $this->cache->expects( $this->once() ) |
||
51 | ->method( 'contains' ) |
||
52 | ->will( $this->returnValue( false ) ); |
||
53 | |||
54 | $this->cachedPropertyValuesPrefetcher->expects( $this->once() ) |
||
55 | ->method( 'getPropertyValues' ) |
||
56 | ->with( |
||
57 | $this->equalTo( $subject->asBase() ), |
||
58 | $this->equalTo( $property ) ) |
||
59 | ->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( true ) ) ) ); |
||
60 | |||
61 | $instance = new EditProtectionValidator( |
||
62 | $this->cachedPropertyValuesPrefetcher, |
||
63 | $this->cache |
||
64 | ); |
||
65 | |||
66 | $this->assertTrue( |
||
67 | $instance->hasProtectionOnNamespace( $subject->getTitle() ) |
||
0 ignored issues
–
show
|
|||
68 | ); |
||
69 | } |
||
70 | |||
71 | public function testHasProtection() { |
||
72 | |||
73 | $subject = $this->dataItemFactory->newDIWikiPage( 'Foo', NS_MAIN, '', 'Bar' ); |
||
74 | $property = $this->dataItemFactory->newDIProperty( '_EDIP' ); |
||
75 | |||
76 | $this->cache->expects( $this->once() ) |
||
77 | ->method( 'contains' ) |
||
78 | ->will( $this->returnValue( false ) ); |
||
79 | |||
80 | $this->cachedPropertyValuesPrefetcher->expects( $this->once() ) |
||
81 | ->method( 'getPropertyValues' ) |
||
82 | ->with( |
||
83 | $this->equalTo( $subject->asBase() ), |
||
84 | $this->equalTo( $property ) ) |
||
85 | ->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( true ) ) ) ); |
||
86 | |||
87 | $instance = new EditProtectionValidator( |
||
88 | $this->cachedPropertyValuesPrefetcher, |
||
89 | $this->cache |
||
90 | ); |
||
91 | |||
92 | $this->assertTrue( |
||
93 | $instance->hasProtection( $subject->getTitle() ) |
||
0 ignored issues
–
show
It seems like
$subject->getTitle() can be null ; however, hasProtection() 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...
|
|||
94 | ); |
||
95 | } |
||
96 | |||
97 | public function testHasProtectionFromCache() { |
||
98 | |||
99 | $subject = $this->dataItemFactory->newDIWikiPage( 'Foo', NS_MAIN, '', 'Bar' ); |
||
100 | |||
101 | $this->cache->expects( $this->once() ) |
||
102 | ->method( 'contains' ) |
||
103 | ->will( $this->returnValue( true ) ); |
||
104 | |||
105 | $this->cache->expects( $this->once() ) |
||
106 | ->method( 'fetch' ) |
||
107 | ->will( $this->returnValue( false ) ); |
||
108 | |||
109 | $instance = new EditProtectionValidator( |
||
110 | $this->cachedPropertyValuesPrefetcher, |
||
111 | $this->cache |
||
112 | ); |
||
113 | |||
114 | $this->assertFalse( |
||
115 | $instance->hasProtection( $subject->getTitle() ) |
||
0 ignored issues
–
show
It seems like
$subject->getTitle() can be null ; however, hasProtection() 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...
|
|||
116 | ); |
||
117 | } |
||
118 | |||
119 | } |
||
120 |
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: