diff --git a/library/fonctions/useragent.php b/library/fonctions/useragent.php index 489eea1f9f0f8df3aa02ba9202817672d8a98744..10d67ce6ff6946fb98088b6e210d276beb384833 100644 --- a/library/fonctions/useragent.php +++ b/library/fonctions/useragent.php @@ -35,7 +35,7 @@ function isTelephone() { function isUserAgentBot($useragent) { - return false !== strpos(strtoupper($useragent), 'BOT/'); + return 0 !== preg_match('/(bot\/|sistrix|voilabot)/i', $useragent); } diff --git a/tests/library/fonctions/UserAgentTest.php b/tests/library/fonctions/UserAgentTest.php index 13c8bdfb0320d3799f74b3e9f999bbcf3aa1422f..6fa6eb8bc767ba03d9d96836431426f91cad890b 100644 --- a/tests/library/fonctions/UserAgentTest.php +++ b/tests/library/fonctions/UserAgentTest.php @@ -42,6 +42,40 @@ class BotUserAgentTest extends PHPUnit_Framework_TestCase { date('H')+1, date('H')+2)); } + + + public function botUserAgents() { + return [ + ['Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'], + ['Mozilla/5.0 (compatible; SISTRIX Crawler; http://crawler.sistrix.net/)'], + ['Mozilla/5.0 (Windows NT 5.1; U; Win64; fr; rv:1.8.1) VoilaBot BETA 1.2 (support.voilabot@orange-ftgroup.com)'], + ]; + } + + /** + * @dataProvider botUserAgents + * @test + */ + public function isUserAgentShouldReturnTrueForAgent($agent) { + $this->assertTrue(isUserAgentBot($agent)); + } + + + public function browserUserAgents() { + return [ + ['Mozilla/5.0'], + ['Webkit'], + ['Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; )'] + ]; + } + + /** + * @dataProvider browserUserAgents + * @test + */ + public function isUserAgentShouldReturnFalseForAgent($agent) { + $this->assertFalse(isUserAgentBot($agent)); + } } ?> \ No newline at end of file