/** * @dataProvider dataProvider */ public function testClassifyLinks($uri, $domains, $in, $out) { # create purifier $config = \HTMLPurifier_Config::createDefault(); $config->set('AutoFormat.Custom', array('ClassifyLinks')); $purifier = new \HTMLPurifier($config); # run test fakeServer($uri, $domains); $this->assertEquals($out, $purifier->purify($in)); }
public function testGetMediaUrl() { # mock class Config $configStub = $this->getMockBuilder('Config')->disableOriginalConstructor()->getMock(); $properties = array(); $configStub->expects($this->any())->method('__get')->will($this->returnCallback(function ($property) use(&$properties) { return $properties[$property]; })); $configStub->expects($this->any())->method('__set')->will($this->returnCallback(function ($property, $value) use(&$properties) { $properties[$property] = $value; return $properties[$property]; })); Config::set($configStub); # exceptions $namespace = 'Studip\\MarkupPrivate\\MediaProxy\\'; $invalidInternalLink = $namespace . 'InvalidInternalLinkException'; $externalMediaDenied = $namespace . 'ExternalMediaDeniedException'; # URLs $sendfile = 'sendfile.php?type=0&file_id=9eea7ca20cba01dd4ea394b3b53027cc&file_name=image.png'; $wiki = 'wiki.php?cid=a07535cf2f8a72df33c12ddfa4b53dde&view=show'; $wikipediaLogo = 'http://upload.wikimedia.org/wikipedia/meta/0/08/Wikipedia-logo-v2_1x.png'; $proxy = 'dispatch.php/media_proxy?url='; $proxiedWikipediaLogo = $proxy . 'http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fmeta%2F0%2F08%2FWikipedia-logo-v2_1x.png'; # domains $domains = array('org' => 'example.org/studip', 'home' => 'example.org/~home', 'net' => 'example.net/studip'); $getUrl = function ($domainKey, $path) use(&$domains) { return 'http://' . $domains[$domainKey] . '/' . $path; }; # run various tests $index = 0; foreach (array(array('in' => $getUrl('org', 'image.jpg'), 'exception' => $invalidInternalLink, 'uri' => $getUrl('org', 'index.php'), 'domains' => $domains, 'externalMedia' => 'allow'), array('in' => $getUrl('org', $sendfile), 'out' => $getUrl('org', $sendfile), 'uri' => $getUrl('org', 'index.php'), 'domains' => $domains, 'externalMedia' => 'allow'), array('in' => $getUrl('org', $sendfile), 'out' => $getUrl('home', $sendfile), 'uri' => $getUrl('home', $wiki), 'domains' => $domains, 'externalMedia' => 'allow'), array('in' => $getUrl('org', $sendfile), 'out' => $getUrl('net', $sendfile), 'uri' => $getUrl('net', $wiki), 'domains' => $domains, 'externalMedia' => 'allow'), array('in' => $wikipediaLogo, 'out' => $wikipediaLogo, 'uri' => $getUrl('org', $wiki), 'domains' => $domains, 'externalMedia' => 'allow'), array('in' => $wikipediaLogo, 'exception' => $externalMediaDenied, 'uri' => $getUrl('org', $wiki), 'domains' => $domains, 'externalMedia' => 'deny'), array('in' => $wikipediaLogo, 'out' => $getUrl('org', $proxiedWikipediaLogo), 'uri' => $getUrl('org', $wiki), 'domains' => $domains, 'externalMedia' => 'proxy')) as $test) { $index++; # fake Stud.IP web server set-up fakeServer($test['uri'], $test['domains']); Config::get()->LOAD_EXTERNAL_MEDIA = $test['externalMedia']; //echoWebGlobals(); // call to help with debugging # test getMediaUrl try { $out = Studip\MarkupPrivate\MediaProxy\getMediaUrl($test['in']); if (isset($test['exception'])) { $this->fail('Test ' . $index . ' did not raise ' . $test['exception'] . '. Output: ' . $out . '.'); } } catch (PHPUnit_Framework_Error_Notice $e) { throw $e; } catch (Exception $e) { if (!isset($test['exception'])) { $this->fail('Test ' . $index . ' raised ' . get_class($e) . '.'); } if (get_class($e) !== $test['exception']) { $this->fail('Test ' . $index . ' raised ' . get_class($e) . ' instead of ' . $test['exception'] . '.'); } } if (isset($test['out'])) { $this->assertEquals($test['out'], $out, 'Test ' . $index); } } }