diff --git a/app/MetaGer.php b/app/MetaGer.php index f64a0473c50fb085d8d3fd76013a21dfb9dcd48c..232542e912814d1e12d35b465bd2f688dfc96bbe 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -541,12 +541,12 @@ class MetaGer public function sumaIsAdsuche($suma, $overtureEnabled) { + $sumaName = $suma["name"]->__toString(); return - $suma["name"]->__toString() === "qualigo" - || $suma["name"]->__toString() === "similar_product_ads" - || (!$overtureEnabled - && $suma["name"]->__toString() === "overtureAds") - || $suma["name"]->__toString() == "rlvproduct"; + $sumaName === "qualigo" + || $sumaName === "similar_product_ads" + || (!$overtureEnabled && $sumaName === "overtureAds") + || $sumaName == "rlvproduct"; } public function sumaIsDisabled($suma) @@ -869,29 +869,36 @@ class MetaGer public function checkSpecialSearches(Request $request) { - $this->searchCheckSitesearch($request); - $this->searchCheckHostBlacklist(); - $this->searchCheckDomainBlacklist(); - $this->searchCheckStopwords(); - $this->searchCheckPhrase(); + $query = $this->q; + if ($request->has('site')) { + $site = $request->input('site'); + } else { + $site = ""; + } + $this->searchCheckSitesearch($query, $site); + $this->searchCheckHostBlacklist($query); + $this->searchCheckDomainBlacklist($query); + $this->searchCheckStopwords($query); + $this->searchCheckPhrase($query); } - public function searchCheckSitesearch($request) + public function searchCheckSitesearch($query, $site) { - if (preg_match("/(.*)\bsite:(\S+)(.*)/si", $this->q, $match)) { + if (preg_match("/(.*)\bsite:(\S+)(.*)/si", $query, $match)) { $this->site = $match[2]; $this->q = $match[1] . $match[3]; } - if ($request->has('site')) { - $this->site = $request->input('site'); + if ($site !== "") { + $this->site = $site; } } - public function searchCheckHostBlacklist() + public function searchCheckHostBlacklist($query) { - while (preg_match("/(.*)(^|\s)-host:(\S+)(.*)/si", $this->q, $match)) { + while (preg_match("/(.*)(^|\s)-host:(\S+)(.*)/si", $query, $match)) { $this->hostBlacklist[] = $match[3]; - $this->q = $match[1] . $match[4]; + $query = $match[1] . $match[4]; + $this->q = $query; } if (sizeof($this->hostBlacklist) > 0) { $hostString = ""; @@ -903,11 +910,12 @@ class MetaGer } } - public function searchCheckDomainBlacklist() + public function searchCheckDomainBlacklist($query) { - while (preg_match("/(.*)(^|\s)-domain:(\S+)(.*)/si", $this->q, $match)) { + while (preg_match("/(.*)(^|\s)-domain:(\S+)(.*)/si", $query, $match)) { $this->domainBlacklist[] = $match[3]; - $this->q = $match[1] . $match[4]; + $query = $match[1] . $match[4]; + $this->q = $query; } if (sizeof($this->domainBlacklist) > 0) { $domainString = ""; @@ -919,11 +927,12 @@ class MetaGer } } - public function searchCheckStopwords() + public function searchCheckStopwords($query) { - while (preg_match("/(.*)(^|\s)-(\S+)(.*)/si", $this->q, $match)) { + while (preg_match("/(.*)(^|\s)-(\S+)(.*)/si", $query, $match)) { $this->stopWords[] = $match[3]; - $this->q = $match[1] . $match[4]; + $query = $match[1] . $match[4]; + $this->q = $query; } if (sizeof($this->stopWords) > 0) { $stopwordsString = ""; @@ -935,10 +944,10 @@ class MetaGer } } - public function searchCheckPhrase() + public function searchCheckPhrase($query) { $p = ""; - $tmp = $this->q; + $tmp = $query; while (preg_match("/(.*)\"(.+)\"(.*)/si", $tmp, $match)) { $tmp = $match[1] . $match[3]; $this->phrases[] = strtolower($match[2]); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 2f2d20ff7238ffc389d60259ba8cf3a5ca95bacb..0000000000000000000000000000000000000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -<?php - -use Illuminate\Foundation\Testing\WithoutMiddleware; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; - -class ExampleTest extends TestCase -{ - /** - * A basic functional test example. - * - * @return void - */ - public function testBasicExample() - { - $this->visit('/') - ->see('Laravel'); - } -} diff --git a/tests/MetaGerPhpTest.php b/tests/MetaGerPhpTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d9befbcb8a8d106f8efbaf28846a609c2de3066c --- /dev/null +++ b/tests/MetaGerPhpTest.php @@ -0,0 +1,109 @@ +<?php + +use App\MetaGer; +use Illuminate\Http\Request; + +class MetaGerPhpTest extends TestCase +{ + /** + * A basic test example. + * + * @return void + */ + public function test() + { + $metager = new MetaGer(); + $request = $this->createDummyRequest(); + $this->fullRunTest($metager, $request); + $this->specialSearchTest($metager); + $this->createSearchEnginesTest($metager); + $this->linkGeneratorTest($metager); + } + + public function fullRunTest($metager, $request) + { + $metager->parseFormData($request); + $metager->checkSpecialSearches($request); + $metager->createSearchEngines($request); + $metager->rankAll(); + $metager->prepareResults(); + $metager->createView(); + } + + public function createDummyRequest() + { + /** + * Constructor. + * + * @param array $query The GET parameters + * @param array $request The POST parameters + * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...) + * @param array $cookies The COOKIE parameters + * @param array $files The FILES parameters + * @param array $server The SERVER parameters + * @param string|resource $content The raw body data + */ + + #new Request(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) + + $query = []; + $query["eingabe"] = "garten"; + $query["focus"] = "angepasst"; + $query["encoding"] = "utf8"; + $query["lang"] = "all"; + $query["time"] = "1000"; + $query["sprueche"] = "on"; + $query["resultCount"] = "20"; + $query["tab"] = "on"; + $query["onenewspage"] = "on"; + + return new Request($query); + } + + public function specialSearchTest($metager) + { + $metager->searchCheckSitesearch("hund katze site:wolf.de", ""); + $this->assertEquals("wolf.de", $metager->getSite()); + $metager->searchCheckHostBlacklist("hund katze -host:wolf.de"); + $this->assertEquals("wolf.de", $metager->getUserHostBlacklist()[0]); + $metager->searchCheckDomainBlacklist("hund katze -domain:wolf.de"); + $this->assertEquals("wolf.de", $metager->getUserDomainBlacklist()[0]); + $metager->searchCheckStopwords("hund katze -wolf"); + $this->assertEquals("wolf", $metager->getStopWords()[0]); + $metager->searchCheckPhrase('hund katze "wolf"'); + $this->assertEquals("wolf", $metager->getPhrases()[0]); + } + + public function createSearchEnginesTest($metager) + { + $this->specialSumaTest($metager); + } + + public function specialSumaTest($metager) + { + $suma = new SimpleXMLElement("<suma></suma>"); + $suma["name"] = "qualigo"; + $this->assertTrue($metager->sumaIsAdsuche($suma, false)); + $suma["disabled"] = "1"; + $this->assertTrue($metager->sumaIsDisabled($suma)); + $suma["name"] = 'overture'; + $this->assertTrue($metager->sumaIsOverture($suma)); + $suma["name"] = 'bing'; + $this->assertTrue($metager->sumaIsNotAdsuche($suma)); + } + + public function linkGeneratorTest($metager) + { + $this->callbackTester($metager, "generateSearchLink", ["bilder"], "http://localhost/meta/meta.ger3?eingabe=garten&focus=bilder&encoding=utf8&lang=all&time=1000&sprueche=on&resultCount=20&tab=on&onenewspage=on&out=results"); + $this->callbackTester($metager, "generateQuicktipLink", [], "http://localhost/qt"); + $this->callbackTester($metager, "generateSiteSearchLink", ["wolf.de"], "http://localhost/meta/meta.ger3?eingabe=garten+site%3Awolf.de&focus=web&encoding=utf8&lang=all&time=1000&sprueche=on&resultCount=20&tab=on&onenewspage=on"); + $this->callbackTester($metager, "generateRemovedHostLink", ["wolf.de"], "http://localhost/meta/meta.ger3?eingabe=garten+-host%3Awolf.de&focus=angepasst&encoding=utf8&lang=all&time=1000&sprueche=on&resultCount=20&tab=on&onenewspage=on"); + $this->callbackTester($metager, "generateRemovedDomainLink", ["wolf.de"], "http://localhost/meta/meta.ger3?eingabe=garten+-domain%3Awolf.de&focus=angepasst&encoding=utf8&lang=all&time=1000&sprueche=on&resultCount=20&tab=on&onenewspage=on"); + } + + public function callbackTester($metager, $funcName, $input, $expectedOutput) + { + $output = call_user_func_array(array($metager, $funcName), $input); + $this->assertEquals($expectedOutput, $output); + } +}