summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/PropertyRestrictionExaminerTest.php
blob: 35983b4c3f5219b2546fd44d5a7c0b6654da48a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace SMW\Tests;

use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\PropertyRestrictionExaminer;

/**
 * @covers \SMW\PropertyRestrictionExaminer
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class PropertyRestrictionExaminerTest extends \PHPUnit_Framework_TestCase {

	private $user;

	protected function setUp() {
		parent::setUp();

		$this->user = $this->getMockBuilder( '\User' )
			->disableOriginalConstructor()
			->getMock();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			PropertyRestrictionExaminer::class,
			new PropertyRestrictionExaminer()
		);
	}

	public function testGrepPropertyFromRestrictionErrorMsg() {

		$msg = '[2,"smw-datavalue-property-create-restriction","Has unknown","foo"]';

		$this->assertInstanceOf(
			DIProperty::class,
			PropertyRestrictionExaminer::grepPropertyFromRestrictionErrorMsg( $msg )
		);
	}

	public function testRestrictionForPredefinedProperty() {

		$instance = new PropertyRestrictionExaminer();

		$instance->checkRestriction( new DIProperty( '_MDAT' )  );

		$this->assertTrue(
			$instance->hasRestriction()
		);
	}

	public function testRestrictionForPredefinedPropertyOnQueryContext() {

		$instance = new PropertyRestrictionExaminer();
		$instance->isQueryContext( true );

		$instance->checkRestriction( new DIProperty( '_MDAT' )  );

		$this->assertFalse(
			$instance->hasRestriction()
		);
	}

	public function testRestrictionForUserPropertyOnFalseCreateProtectionRight() {

		$instance = new PropertyRestrictionExaminer();

		$instance->setCreateProtectionRight( false );
		$instance->setUser( $this->user );

		$instance->checkRestriction( new DIProperty( 'Foo' )  );

		$this->assertFalse(
			$instance->hasRestriction()
		);
	}

	public function testRestrictionForUserPropertyOnCreateProtectionRight() {

		$instance = new PropertyRestrictionExaminer();

		$instance->setCreateProtectionRight( 'foo' );
		$instance->setUser( $this->user );

		$instance->checkRestriction( new DIProperty( 'Foo' )  );

		$this->assertTrue(
			$instance->hasRestriction()
		);
	}

	public function testRestrictionForUserPropertyOnCreateProtectionRightWithAllowedUser() {

		$right = 'bar';

		$this->user->expects( $this->once() )
			->method( 'isAllowed' )
			->with( $this->equalTo( $right ) )
			->will( $this->returnValue( true ) );

		$instance = new PropertyRestrictionExaminer();

		$instance->setCreateProtectionRight( $right );
		$instance->setUser( $this->user );

		$instance->checkRestriction( new DIProperty( 'Foo' )  );

		$this->assertFalse(
			$instance->hasRestriction()
		);
	}

	public function testDeclarativePropertyOnMainNamespace() {

		$instance = new PropertyRestrictionExaminer();

		$instance->checkRestriction(
			new DIProperty( '_TYPE' ),
			DIWikiPage::newFromText( 'Bar', NS_MAIN )
		);

		$this->assertTrue(
			$instance->hasRestriction()
		);
	}

	public function testDeclarativePropertyOnPropertyNamespace() {

		$instance = new PropertyRestrictionExaminer();

		$instance->checkRestriction(
			new DIProperty( '_TYPE' ),
			DIWikiPage::newFromText( 'Bar', SMW_NS_PROPERTY )
		);

		$this->assertFalse(
			$instance->hasRestriction()
		);
	}

}