diff --git a/controllers/API.php b/controllers/API.php index be65c20..ae9fc63 100644 --- a/controllers/API.php +++ b/controllers/API.php @@ -105,7 +105,7 @@ class API { $domain = parse_url($url, PHP_URL_HOST); if($url == $target || $domain == $target_domain || # subdomain check - ($target_domain and substr_compare($domain, '.' . $target_domain, -(strlen($target_domain) + 1)) == 0)) { + ($target_domain and $domain and substr_compare($domain, '.' . $target_domain, -(strlen($target_domain) + 1)) == 0)) { $found[$url] = null; } } diff --git a/tests/APITest.php b/tests/APITest.php index 200155b..526ce28 100644 --- a/tests/APITest.php +++ b/tests/APITest.php @@ -52,9 +52,8 @@ class APITest extends PHPUnit_Framework_TestCase { # Verify it queued the mention in the database $d = ORM::for_table('webmentions')->where(['source' => $source, 'target' => $target])->find_one(); - $this->assertNotNull($d); + $this->assertNotFalse($d); $this->assertEquals($match[1], $d->token); - # Check the status endpoint to make sure it says it's still queued $response = $this->status($d->token); $this->assertEquals(200, $response->getStatusCode()); @@ -62,6 +61,12 @@ class APITest extends PHPUnit_Framework_TestCase { $this->assertEquals('queued', $data->status); } + private function _assertNotQueued($source, $target) { + # Verify it did not queue a mention in the database + $d = ORM::for_table('webmentions')->where(['source' => $source, 'target' => $target])->find_one(); + $this->assertFalse($d); + } + public function testAuthentication() { $response = $this->webmention([]); $this->assertEquals(401, $response->getStatusCode()); @@ -192,6 +197,23 @@ class APITest extends PHPUnit_Framework_TestCase { $this->_assertQueued('http://source.example.com/basictest', 'http://target2.example.com', $data->location[1]); } + public function testTargetDomainQueuesOnlyWebmentionsFromTargetDomain() { + $this->_createExampleAccount(); + + $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/multipletest','target_domain'=>'example.com']); + $body = $response->getContent(); + $this->assertEquals(201, $response->getStatusCode(), $body); + $data = json_decode($body); + $this->assertEquals(false, property_exists($data, 'error'), $body); + $this->assertEquals('queued', $data->status, $body); + $this->assertEquals(2, count($data->location), $body); + $this->_assertQueued('http://source.example.com/multipletest', 'http://target.example.com', $data->location[0]); + $this->_assertQueued('http://source.example.com/multipletest', 'http://target2.example.com', $data->location[1]); + $this->_assertNotQueued('http://source.example.com/multipletest', 'http://target.example.org'); + $this->_assertNotQueued('http://source.example.com/multipletest', '/relativelink'); + $this->_assertNotQueued('http://source.example.com/multipletest', 'http://source.example.com/relativelink'); + } + public function testStatusNotFound() { $this->_createExampleAccount();