Issues (65)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/unit/Lookup/InMemoryEntityLookupTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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 Wikibase\DataModel\Services\Tests\Lookup;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument;
10
use Wikibase\DataModel\Services\Fixtures\ItemFixtures;
11
use Wikibase\DataModel\Services\Fixtures\PropertyFixtures;
12
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
13
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
14
use Wikibase\DataModel\Services\Lookup\ItemLookupException;
15
use Wikibase\DataModel\Services\Lookup\PropertyLookupException;
16
17
/**
18
 * @covers \Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup
19
 *
20
 * @license GPL-2.0-or-later
21
 * @author Jeroen De Dauw < [email protected] >
22
 */
23
class InMemoryEntityLookupTest extends TestCase {
24
25
	public function testGivenUnknownEntityId_getEntityReturnsNull() {
26
		$lookup = new InMemoryEntityLookup();
27
		$this->assertNull( $lookup->getEntity( new ItemId( 'Q1' ) ) );
28
	}
29
30
	public function testGivenKnownEntityId_getEntityReturnsTheEntity() {
31
		$lookup = new InMemoryEntityLookup();
32
33
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
34
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
35
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
36
37
		$this->assertEquals(
38
			new FakeEntityDocument( new ItemId( 'Q2' ) ),
39
			$lookup->getEntity( new ItemId( 'Q2' ) )
40
		);
41
	}
42
43
	public function testGivenUnknownEntityId_hasEntityReturnsFalse() {
44
		$lookup = new InMemoryEntityLookup();
45
		$this->assertFalse( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
46
	}
47
48
	public function testGivenKnownEntityId_hasEntityReturnsTrue() {
49
		$lookup = new InMemoryEntityLookup();
50
51
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
52
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
53
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
54
55
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
56
	}
57
58
	public function testGivenIdInExceptionList_getEntityThrowsException() {
59
		$lookup = new InMemoryEntityLookup();
60
61
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
62
63
		$lookup->getEntity( new ItemId( 'Q2' ) );
64
		$this->expectException( EntityLookupException::class );
65
		$lookup->getEntity( new ItemId( 'Q1' ) );
66
	}
67
68
	public function testGivenIdInExceptionList_hasEntityThrowsException() {
69
		$lookup = new InMemoryEntityLookup();
70
71
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
72
73
		$lookup->hasEntity( new ItemId( 'Q2' ) );
74
		$this->expectException( EntityLookupException::class );
75
		$lookup->hasEntity( new ItemId( 'Q1' ) );
76
	}
77
78
	public function testGivenConstructorVarArgEntities_theyCanBeRetrieved() {
79
		$lookup = new InMemoryEntityLookup(
80
			new FakeEntityDocument( new ItemId( 'Q1' ) ),
81
			new FakeEntityDocument( new ItemId( 'Q2' ) )
82
		);
83
84
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
85
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
86
	}
87
88
	public function testGivenEntityWithoutIdInConstructor_exceptionIsThrown() {
89
		$this->expectException( InvalidArgumentException::class );
90
91
		new InMemoryEntityLookup(
92
			new FakeEntityDocument()
93
		);
94
	}
95
96
	public function testGivenKnownItem_getItemForIdReturnsIt() {
97
		$item = ItemFixtures::newItem();
98
99
		$lookup = new InMemoryEntityLookup( $item );
100
101
		$this->assertEquals(
102
			$item,
103
			$lookup->getItemForId( $item->getId() )
0 ignored issues
show
It seems like $item->getId() can be null; however, getItemForId() 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...
104
		);
105
	}
106
107
	public function testGivenKnownProperty_getPropertyForIdReturnsIt() {
108
		$property = PropertyFixtures::newProperty();
109
110
		$lookup = new InMemoryEntityLookup( $property );
111
112
		$this->assertEquals(
113
			$property,
114
			$lookup->getPropertyForId( $property->getId() )
0 ignored issues
show
It seems like $property->getId() can be null; however, getPropertyForId() 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...
115
		);
116
	}
117
118
	public function testWhenItemIsNotKnown_getItemForIdReturnsNull() {
119
		$this->assertNull(
120
			( new InMemoryEntityLookup() )->getItemForId( new ItemId( 'Q1' ) )
121
		);
122
	}
123
124
	public function testWhenPropertyIsNotKnown_getPropertyForIdReturnsNull() {
125
		$this->assertNull(
126
			( new InMemoryEntityLookup() )->getPropertyForId( new PropertyId( 'P1' ) )
127
		);
128
	}
129
130
	public function testGetItemForIdThrowsCorrectExceptionType() {
131
		$id = new ItemId( 'Q1' );
132
133
		$lookup = new InMemoryEntityLookup();
134
		$lookup->addException( new EntityLookupException( $id ) );
135
136
		$this->expectException( ItemLookupException::class );
137
		$lookup->getItemForId( $id );
138
	}
139
140
	public function testGetPropertyForIdThrowsCorrectExceptionType() {
141
		$id = new PropertyId( 'P1' );
142
143
		$lookup = new InMemoryEntityLookup();
144
		$lookup->addException( new EntityLookupException( $id ) );
145
146
		$this->expectException( PropertyLookupException::class );
147
		$lookup->getPropertyForId( $id );
148
	}
149
150
}
151