Skip to content

Commit c308749

Browse files
author
Tom
committed
Merge pull request #11 from tomverran/add-match-tests
Add match tests
2 parents 2fd3109 + 2c0f0ee commit c308749

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Robot/UserAgent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public function getMatches($userAgent)
2121
}
2222

2323
$matches = array_filter($this->userAgents, function($elem) use($ua) {
24-
return strpos($elem, $ua) !== false || $elem == '*';
24+
return strpos($elem, $ua) !== false || strpos($ua, $elem) !== false || $elem == '*';
2525
});
26-
return $matches;
26+
return array_values($matches);
2727
}
2828
}

tests/RecordTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,22 @@ public function givenUserAgentMatch_delegateToAccessRules()
3636
$this->assertFalse((new Record($alwaysMatchingUserAgent, $this->fooForbidden))->isAllowed('Googlebot', '/foo'));
3737
$this->assertTrue((new Record($alwaysMatchingUserAgent, $this->allUrlsAllowed))->isAllowed('Googlebot', '/foo'));
3838
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function givenNoMatches_returnMatchStrengthOfZero()
44+
{
45+
$googleOnly = new Record(new UserAgent(['Googlebot']), new AccessRules([]));
46+
$this->assertTrue($googleOnly->getMatchStrength('Bing') == 0, 'No match at all means a match strength of zero');
47+
}
48+
49+
/**
50+
* @test
51+
*/
52+
public function givenMatch_returnLengthOfMatchedUAAsMatchStrength()
53+
{
54+
$googleOnly = new Record(new UserAgent(['Googlebot']), new AccessRules([]));
55+
$this->assertTrue($googleOnly->getMatchStrength('G') == 9, 'Length of the matched UA is the strength');
56+
}
3957
}

tests/UserAgentTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,17 @@ public function givenWildcardAgent_alwaysMatch()
9393
$this->assertTrue($wildcard->getMatches($ua) == ['*'], 'wilcard matches all');
9494
}
9595
}
96+
97+
/**
98+
* @test
99+
* https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt#order-of-precedence-for-user-agents
100+
*/
101+
public function givenGoogleExamples_behaveAsExpected()
102+
{
103+
$googleUAs = new UserAgent(['googlebot-news', '*', 'googlebot']);
104+
$this->assertContains('googlebot-news', $googleUAs->getMatches('Googlebot-News'));
105+
$this->assertContains('googlebot', $googleUAs->getMatches('Googlebot-Images'));
106+
$this->assertNotContains('googlebot-news', $googleUAs->getMatches('Googlebot-Images'));
107+
$this->assertEquals(['*'], $googleUAs->getMatches('otherbot'));
108+
}
96109
}

0 commit comments

Comments
 (0)