From 24759fe2803ae365bfdd16651434ff7d441eca4f Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Thu, 10 Nov 2016 14:03:59 +1100 Subject: [PATCH] Issue #22 - Allowing tests to point to a file when a local URL was found. It will be used to test parsed contents later (parse CSS for example). --- .../controllers/maintenance_static_page.php | 32 +++++++++++--- .../local/controllers/fixtures/catalyst.png | Bin 0 -> 2867 bytes .../local/controllers/fixtures/simple.css | 7 +++ .../maintenance_static_page_test.php | 41 ++++++++++-------- 4 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 tests/phpunit/local/controllers/fixtures/catalyst.png create mode 100644 tests/phpunit/local/controllers/fixtures/simple.css diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index 0cad11e..ad5b2e4 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -61,7 +61,7 @@ class maintenance_static_page { } else if (PHPUNIT_TEST) { $html = ''; } else { - $html = file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id); + $html = self::file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id); } return self::create_from_html($html); @@ -92,6 +92,20 @@ class maintenance_static_page { return new maintenance_static_page($dom); } + /** + * Tries to get the contents of the file or URL. + * @param string $file File to get. + * @return string Contents of $file or an empty string if failed. + */ + private static function file_get_contents($file) { + $contents = @file_get_contents($file); + if ($contents === false) { + debugging('Cannot fetch: '.$file); + return ''; // Better a broken link than halting the generation. + } + return $contents; + } + /** @var DOMDocument */ protected $dom; @@ -259,12 +273,20 @@ class maintenance_static_page { if (is_null($filename)) { $url = $href; // Skipped, use original URL. } else { + $this->update_link_stylesheet_parse($filename); $url = $this->get_url_for_file($filename); } $link->setAttribute('href', $url); } } + /** + * Checks for urls inside filename. + * @param string $filename + */ + private function update_link_stylesheet_parse($filename) { + } + /** * Fetch and fixes the favicon link tag. */ @@ -337,12 +359,12 @@ class maintenance_static_page { return null; // External URL, leave it. } - // PHPUnit will use www.example.com as wwwroot and we don't to copy the file. + // PHPUnit does not expose a web interface to fetch, point to local file instead. if (PHPUNIT_TEST) { - $contents = ''; - } else { - $contents = file_get_contents($url); + $url = str_replace($CFG->wwwroot, $CFG->dirroot, $url); } + + $contents = self::file_get_contents($url); $filename = sha1($contents).'.'.$type; $filepath = $this->get_resources_folder().'/'.$filename; file_put_contents($filepath, $contents); diff --git a/tests/phpunit/local/controllers/fixtures/catalyst.png b/tests/phpunit/local/controllers/fixtures/catalyst.png new file mode 100644 index 0000000000000000000000000000000000000000..e241f49dadf76f22114435f9bdc3b0a39c7141fb GIT binary patch literal 2867 zcmV-33(WM1P)00006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY4c7nw4c7reD4Tcy000McNliru;0p=@IXRH{v5Noz3cyK3 zK~#9!?VWvaRMi>ApZDxXNP@Nz$6^fGyK?UersHViR0~Rp`9LY4PDivYYR6iq*0FUI z(NV`qTdMd`q-8+GGJsS*qKFtol5ALKG}P&cwQ7na3)w{yn~qpYNg$iM_w650kdFYV zd+$B><~)CHW|MQCU*2=x^G!H~nj*$n5MK+X@gS4{G?IWkIHR9erP@}(BZ_OWIRNgE zty~Q54G#3fhNv+?FrEN#IY0n?>1)oE@7A4Px|T3pZTSWdx+j$X0x`brep|@1 zsyaW}Ep;T9m4&lCovRqQ-Ybb>@PCOH7(bGrJRh{AlS^ebY=f@ng?citGB6n)k^q21 z*|qB;w(gZV0ei2!JQOm;YIqDX0EkVcdfCAtlJCEzA_R04!?5{yQltJ<0TyR75n@tW~0lQ**Kll9TXE|+0X zFi-qSR>*P9SO5v9HUAILG}7I!8NaJ&Yx`VAbK()>NdR}k2ND2>T@0De^TXozG8)sU z8Bc&P6F!k}`L925#kOt#k%*YCgPPK7h5>L zfhHjJuY%ArSqx~7j2_2~wQz2G)X?aoE#gO~x3*>^8A4rb0dD{(gwN%0L~~oHqhzHF z6|VyVU_1=q3=9D|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aH&RReeMPf?;zFA?BtdAzJ zmaFD^yk;x_DadkW-GrK46e1omo&@kSg+t!q)kRHFefn1#LTRzbL71sf$PF4`THdG` z&&8v$D|wJZp@T2c@Pi72{mS)8jhzHq89-*vsU9|qYq1*{yaH;2y%Wm$9VVp=zHNUn zc+w4m4N+qPgO@4Do*74#Xro{X#Ik#^5|oH{Nq4BOlaCj z7WC#&zpl{-pt-)XWx3ZQnvr~`qrG{c z3>&~b%5Fb^CSF(zSjT?6DSE*L3|^$H_6O)p*34UWT)$jaHayGLYXHtvR{H`pZ@79_ z>v3;tnEU$X0vMxg_6cYvtT^_w#;9HcB6a_lk3jD|_Dpm_)4mRi7DH_V@(*YNUOo0q zw29zzL?}`A`e__}Otak-F@6Z(MrEzPBKn_!j`I?So9tNZY!K!vTm1)mMFrk64>#Ev z{w;j`*!B~_exfY(AL#CuRPrytp$k}v=0TFDkd$(mj1hBv?V%~pc*OVt z)PjXSBia(E2M)d#2g2jZN`Hdh-`Ce02Y~$W@CTsQnEeTwfcVs|uHN{0V@5DoEkAk$ zbpPN<)4?gvkaa)Q%9QjXP==Z!#z%v^9F>Jhqs+W)zsQOm01B0r(rc>#p>vR5darU< zTcJ)7~C7{kRdudqpa@CHu_+iih5Twj6z&PnU&z4CagK3Jg&=dMF zP#C)HNN#mV6{*x75@r>EtplX!`$Pz1K#n{vuJjclTLEiIMQiK6<3IyIRYym6e~>Q+ z;iVI$=&)FA5%NHD9V`rwpR|4Zr$>K2fEuIvG-9d+a0vhq^IRsnS6L~oJ|M!Zs*d)i z6aNF8EcpVcIig=~5vH;b8Da4Z0;#P+GE9Piy8-%)ncoS3U#mget_fixture_path('simple.css'); + $external_css_link = 'http://google.com/coolstuff.css'; $html = "\n". - 'Title'. - 'Content'; + 'Title'. + 'Content'; $generated = $this->generated_page_html($html); self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated); - self::assertNotContains($link1, $generated); - self::assertNotContains($link2, $generated); - self::assertContains($link3, $generated); + self::assertNotContains($local_css_link, $generated); + self::assertContains($external_css_link, $generated); } public function test_updateimages() { - $link1 = (string)new moodle_url('/example.png'); - $link2 = (string)new moodle_url('/auth/outage/imagefile'); - $link3 = 'http://google.com/coolstyle.css'; + $local_img_link = $this->get_fixture_path('catalyst.png'); + $external_img_link = 'http://google.com/coolstyle.css'; $html = "\n". - 'an imageTitle'. - 'Content'; + 'Title'. + 'Content'; $generated = $this->generated_page_html($html); self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated); - self::assertNotContains($link1, $generated); - self::assertNotContains($link2, $generated); - self::assertContains($link3, $generated); + self::assertNotContains($local_img_link, $generated); + self::assertContains($external_img_link, $generated); } public function test_updatelinkfavicon() { - $link = (string)new moodle_url('/favicon.jpg'); + $link = $this->get_fixture_path('catalyst.png'); $html = "\n". 'Title'. 'Content'; @@ -116,7 +112,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase { } public function test_previewpath() { - $link = (string)new moodle_url('/favicon.jpg'); + $link = $this->get_fixture_path('catalyst.png'); $html = "\n". 'Title'. 'Content'; @@ -162,4 +158,13 @@ class maintenance_static_page_test extends auth_outage_base_testcase { maintenance_static_page::create_from_outage(null)->generate(); self::assertFileNotExists($file); } + + /** + * Gets a fixture file for this test case. + * @param $file + * @return string + */ + private function get_fixture_path($file) { + return (string)new moodle_url('/auth/outage/tests/phpunit/local/controllers/fixtures/'.$file); + } }