123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036 |
- <?php
- class DrupalAlterTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'drupal_alter() tests',
- 'description' => 'Confirm that alteration of arguments passed to drupal_alter() works correctly.',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('common_test');
- }
- function testDrupalAlter() {
-
-
- global $theme, $base_theme_info;
- $theme = 'bartik';
- $base_theme_info = array();
- $array = array('foo' => 'bar');
- $entity = new stdClass();
- $entity->foo = 'bar';
-
- $array_copy = $array;
- $array_expected = array('foo' => 'Drupal theme');
- drupal_alter('drupal_alter', $array_copy);
- $this->assertEqual($array_copy, $array_expected, 'Single array was altered.');
- $entity_copy = clone $entity;
- $entity_expected = clone $entity;
- $entity_expected->foo = 'Drupal theme';
- drupal_alter('drupal_alter', $entity_copy);
- $this->assertEqual($entity_copy, $entity_expected, 'Single object was altered.');
-
- $array_copy = $array;
- $array_expected = array('foo' => 'Drupal theme');
- $entity_copy = clone $entity;
- $entity_expected = clone $entity;
- $entity_expected->foo = 'Drupal theme';
- $array2_copy = $array;
- $array2_expected = array('foo' => 'Drupal theme');
- drupal_alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
- $this->assertEqual($array_copy, $array_expected, 'First argument to drupal_alter() was altered.');
- $this->assertEqual($entity_copy, $entity_expected, 'Second argument to drupal_alter() was altered.');
- $this->assertEqual($array2_copy, $array2_expected, 'Third argument to drupal_alter() was altered.');
-
-
-
- $array_copy = $array;
- $array_expected = array('foo' => 'Drupal block theme');
- drupal_alter(array('drupal_alter', 'drupal_alter_foo'), $array_copy);
- $this->assertEqual($array_copy, $array_expected, 'hook_TYPE_alter() implementations ran in correct order.');
- }
- }
- class CommonURLUnitTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'URL generation tests',
- 'description' => 'Confirm that url(), drupal_get_query_parameters(), drupal_http_build_query(), and l() work correctly with various input.',
- 'group' => 'System',
- );
- }
-
- function testLXSS() {
- $text = $this->randomName();
- $path = "<SCRIPT>alert('XSS')</SCRIPT>";
- $link = l($text, $path);
- $sanitized_path = check_url(url($path));
- $this->assertTrue(strpos($link, $sanitized_path) !== FALSE, format_string('XSS attack @path was filtered', array('@path' => $path)));
- }
-
- function testLActiveClass() {
- $link = l($this->randomName(), $_GET['q']);
- $this->assertTrue($this->hasClass($link, 'active'), format_string('Class @class is present on link to the current page', array('@class' => 'active')));
- }
-
- function testLCustomClass() {
- $class = $this->randomName();
- $link = l($this->randomName(), $_GET['q'], array('attributes' => array('class' => array($class))));
- $this->assertTrue($this->hasClass($link, $class), format_string('Custom class @class is present on link when requested', array('@class' => $class)));
- $this->assertTrue($this->hasClass($link, 'active'), format_string('Class @class is present on link to the current page', array('@class' => 'active')));
- }
- private function hasClass($link, $class) {
- return preg_match('|class="([^\"\s]+\s+)*' . $class . '|', $link);
- }
-
- function testDrupalGetQueryParameters() {
- $original = array(
- 'a' => 1,
- 'b' => array(
- 'd' => 4,
- 'e' => array(
- 'f' => 5,
- ),
- ),
- 'c' => 3,
- 'q' => 'foo/bar',
- );
-
- $result = $_GET;
- unset($result['q']);
- $this->assertEqual(drupal_get_query_parameters(), $result, "\$_GET['q'] was removed.");
-
- $result = $original;
- unset($result['q']);
- $this->assertEqual(drupal_get_query_parameters($original), $result, "'q' was removed.");
-
- $result = $original;
- unset($result['b']);
- $this->assertEqual(drupal_get_query_parameters($original, array('b')), $result, "'b' was removed.");
-
- $result = $original;
- unset($result['b']['d']);
- $this->assertEqual(drupal_get_query_parameters($original, array('b[d]')), $result, "'b[d]' was removed.");
-
- $result = $original;
- unset($result['b']['e']['f']);
- $this->assertEqual(drupal_get_query_parameters($original, array('b[e][f]')), $result, "'b[e][f]' was removed.");
-
- $result = $original;
- unset($result['a'], $result['b']['e'], $result['c']);
- $this->assertEqual(drupal_get_query_parameters($original, array('a', 'b[e]', 'c')), $result, "'a', 'b[e]', 'c' were removed.");
- }
-
- function testDrupalHttpBuildQuery() {
- $this->assertEqual(drupal_http_build_query(array('a' => ' &#//+%20@۞')), 'a=%20%26%23//%2B%2520%40%DB%9E', 'Value was properly encoded.');
- $this->assertEqual(drupal_http_build_query(array(' &#//+%20@۞' => 'a')), '%20%26%23%2F%2F%2B%2520%40%DB%9E=a', 'Key was properly encoded.');
- $this->assertEqual(drupal_http_build_query(array('a' => '1', 'b' => '2', 'c' => '3')), 'a=1&b=2&c=3', 'Multiple values were properly concatenated.');
- $this->assertEqual(drupal_http_build_query(array('a' => array('b' => '2', 'c' => '3'), 'd' => 'foo')), 'a[b]=2&a[c]=3&d=foo', 'Nested array was properly encoded.');
- }
-
- function testDrupalParseUrl() {
-
- $url = 'foo/bar?foo=bar&bar=baz&baz#foo';
- $result = array(
- 'path' => 'foo/bar',
- 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''),
- 'fragment' => 'foo',
- );
- $this->assertEqual(drupal_parse_url($url), $result, 'Relative URL parsed correctly.');
-
- $url = 'foo/bar:1';
- $result = array(
- 'path' => 'foo/bar:1',
- 'query' => array(),
- 'fragment' => '',
- );
- $this->assertEqual(drupal_parse_url($url), $result, 'Relative URL parsed correctly.');
-
- $url = '/foo/bar?foo=bar&bar=baz&baz#foo';
- $result = array(
- 'path' => '/foo/bar',
- 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''),
- 'fragment' => 'foo',
- );
- $this->assertEqual(drupal_parse_url($url), $result, 'Absolute URL parsed correctly.');
-
- $url = 'http://drupal.org/foo/bar?foo=bar&bar=baz&baz#foo';
-
- $this->assertTrue(url_is_external($url), 'Correctly identified an external URL.');
-
- $url = '//drupal.org/foo/bar?foo=bar&bar=baz&baz#foo';
- $this->assertTrue(url_is_external($url), 'Correctly identified an external URL without a protocol part.');
-
- $url = '/drupal.org';
- $this->assertFalse(url_is_external($url), 'Correctly identified an internal URL with a leading slash.');
-
- $url = 'http://drupal.org/foo/bar?foo=bar&bar=baz&baz#foo';
- $result = array(
- 'path' => 'http://drupal.org/foo/bar',
- 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''),
- 'fragment' => 'foo',
- );
- $this->assertEqual(drupal_parse_url($url), $result, 'External URL parsed correctly.');
-
- $result = array(
- 'path' => 'foo/bar',
- 'query' => array('bar' => 'baz'),
- 'fragment' => 'foo',
- );
-
- $url = $GLOBALS['base_url'] . '/?q=foo/bar&bar=baz#foo';
- $this->assertEqual(drupal_parse_url($url), $result, 'Absolute URL with clean URLs disabled parsed correctly.');
-
- $url = '?q=foo/bar&bar=baz#foo';
- $this->assertEqual(drupal_parse_url($url), $result, 'Relative URL with clean URLs disabled parsed correctly.');
-
- $url = 'index.php?q=foo/bar&bar=baz#foo';
- $this->assertEqual(drupal_parse_url($url), $result, 'Relative URL on non-Apache webserver with clean URLs disabled parsed correctly.');
-
- $parts = drupal_parse_url('forged:http://cwe.mitre.org/data/definitions/601.html');
- $this->assertFalse(valid_url($parts['path'], TRUE), 'drupal_parse_url() correctly parsed a forged URL.');
- }
-
- function testUrl() {
- global $base_url;
- foreach (array(FALSE, TRUE) as $absolute) {
-
- $base = $absolute ? $base_url . '/' : base_path();
- $absolute_string = $absolute ? 'absolute' : NULL;
-
- $GLOBALS['conf']['clean_url'] = 0;
- $url = $base . '?q=node/123';
- $result = url('node/123', array('absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123#foo';
- $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123&foo';
- $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123&foo=bar&bar=baz';
- $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123&foo#bar';
- $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123&foo#0';
- $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '0', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . '?q=node/123&foo';
- $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base;
- $result = url('<front>', array('absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
-
- $GLOBALS['conf']['clean_url'] = 1;
- $url = $base . 'node/123';
- $result = url('node/123', array('absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . 'node/123#foo';
- $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . 'node/123?foo';
- $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . 'node/123?foo=bar&bar=baz';
- $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base . 'node/123?foo#bar';
- $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- $url = $base;
- $result = url('<front>', array('absolute' => $absolute));
- $this->assertEqual($url, $result, "$url == $result");
- }
- }
-
- function testExternalUrls() {
- $test_url = 'http://drupal.org/';
-
- $url = $test_url . '#drupal';
- $result = url($url);
- $this->assertEqual($url, $result, 'External URL with fragment works without a fragment in $options.');
-
- $url = $test_url . '#drupal';
- $fragment = $this->randomName(10);
- $result = url($url, array('fragment' => $fragment));
- $this->assertEqual($test_url . '#' . $fragment, $result, 'External URL fragment is overidden with a custom fragment in $options.');
-
- $url = $test_url . '?drupal=awesome';
- $result = url($url);
- $this->assertEqual($url, $result, 'External URL with query string works without a query string in $options.');
-
- $url = $test_url;
- $query = array($this->randomName(5) => $this->randomName(5));
- $result = url($url, array('query' => $query));
- $this->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, 'External URL can be extended with a query string in $options.');
-
- $url = $test_url . '?drupal=awesome';
- $query = array($this->randomName(5) => $this->randomName(5));
- $result = url($url, array('query' => $query));
- $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result, 'External URL query string can be extended with a custom query string in $options.');
-
-
- $url = '/drupal.org';
- $result = url($url);
- $this->assertTrue(strpos($result, '//') === FALSE, 'Internal URL does not turn into an external URL.');
-
- $url = '//drupal.org';
- $result = url($url);
- $this->assertEqual($url, $result, 'External URL without protocol is not altered.');
- }
- }
- class CommonXssUnitTest extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'String filtering tests',
- 'description' => 'Confirm that check_plain(), filter_xss(), format_string() and check_url() work correctly, including invalid multi-byte sequences.',
- 'group' => 'System',
- );
- }
-
- function testInvalidMultiByte() {
-
- $text = @check_plain("Foo\xC0barbaz");
- $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "Foo\xC0barbaz"');
-
- $text = @check_plain("\xc2\"");
- $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "\xc2\""');
- $text = check_plain("Fooÿñ");
- $this->assertEqual($text, "Fooÿñ", 'check_plain() accepts valid sequence "Fooÿñ"');
- $text = filter_xss("Foo\xC0barbaz");
- $this->assertEqual($text, '', 'filter_xss() rejects invalid sequence "Foo\xC0barbaz"');
- $text = filter_xss("Fooÿñ");
- $this->assertEqual($text, "Fooÿñ", 'filter_xss() accepts valid sequence Fooÿñ');
- }
-
- function testEscaping() {
- $text = check_plain("<script>");
- $this->assertEqual($text, '<script>', 'check_plain() escapes <script>');
- $text = check_plain('<>&"\'');
- $this->assertEqual($text, '<>&"'', 'check_plain() escapes reserved HTML characters.');
- }
-
- function testFormatStringAndT() {
- foreach (array('format_string', 't') as $function) {
- $text = $function('Simple text');
- $this->assertEqual($text, 'Simple text', $function . ' leaves simple text alone.');
- $text = $function('Escaped text: @value', array('@value' => '<script>'));
- $this->assertEqual($text, 'Escaped text: <script>', $function . ' replaces and escapes string.');
- $text = $function('Placeholder text: %value', array('%value' => '<script>'));
- $this->assertEqual($text, 'Placeholder text: <em class="placeholder"><script></em>', $function . ' replaces, escapes and themes string.');
- $text = $function('Verbatim text: !value', array('!value' => '<script>'));
- $this->assertEqual($text, 'Verbatim text: <script>', $function . ' replaces verbatim string as-is.');
- }
- }
-
- function testBadProtocolStripping() {
-
-
-
- $url = 'javascript:http://www.example.com/?x=1&y=2';
- $expected_plain = 'http://www.example.com/?x=1&y=2';
- $expected_html = 'http://www.example.com/?x=1&y=2';
- $this->assertIdentical(check_url($url), $expected_html, 'check_url() filters a URL and encodes it for HTML.');
- $this->assertIdentical(drupal_strip_dangerous_protocols($url), $expected_plain, 'drupal_strip_dangerous_protocols() filters a URL and returns plain text.');
- }
- }
- class CommonSizeTestCase extends DrupalUnitTestCase {
- protected $exact_test_cases;
- protected $rounded_test_cases;
- public static function getInfo() {
- return array(
- 'name' => 'Size parsing test',
- 'description' => 'Parse a predefined amount of bytes and compare the output with the expected value.',
- 'group' => 'System'
- );
- }
- function setUp() {
- $kb = DRUPAL_KILOBYTE;
- $this->exact_test_cases = array(
- '1 byte' => 1,
- '1 KB' => $kb,
- '1 MB' => $kb * $kb,
- '1 GB' => $kb * $kb * $kb,
- '1 TB' => $kb * $kb * $kb * $kb,
- '1 PB' => $kb * $kb * $kb * $kb * $kb,
- '1 EB' => $kb * $kb * $kb * $kb * $kb * $kb,
- '1 ZB' => $kb * $kb * $kb * $kb * $kb * $kb * $kb,
- '1 YB' => $kb * $kb * $kb * $kb * $kb * $kb * $kb * $kb,
- );
- $this->rounded_test_cases = array(
- '2 bytes' => 2,
- '1 MB' => ($kb * $kb) - 1,
- round(3623651 / ($this->exact_test_cases['1 MB']), 2) . ' MB' => 3623651,
- round(67234178751368124 / ($this->exact_test_cases['1 PB']), 2) . ' PB' => 67234178751368124,
- round(235346823821125814962843827 / ($this->exact_test_cases['1 YB']), 2) . ' YB' => 235346823821125814962843827,
- );
- parent::setUp();
- }
-
- function testCommonFormatSize() {
- foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) {
- foreach ($test_cases as $expected => $input) {
- $this->assertEqual(
- ($result = format_size($input, NULL)),
- $expected,
- $expected . ' == ' . $result . ' (' . $input . ' bytes)'
- );
- }
- }
- }
-
- function testCommonParseSize() {
- foreach ($this->exact_test_cases as $string => $size) {
- $this->assertEqual(
- $parsed_size = parse_size($string),
- $size,
- $size . ' == ' . $parsed_size . ' (' . $string . ')'
- );
- }
-
- $string = '23476892 bytes';
- $this->assertEqual(
- ($parsed_size = parse_size($string)),
- $size = 23476892,
- $string . ' == ' . $parsed_size . ' bytes'
- );
- $string = '76MRandomStringThatShouldBeIgnoredByParseSize.';
- $this->assertEqual(
- $parsed_size = parse_size($string),
- $size = 79691776,
- $string . ' == ' . $parsed_size . ' bytes'
- );
- $string = '76.24 Giggabyte';
- $this->assertEqual(
- $parsed_size = parse_size($string),
- $size = 81862076662,
- $string . ' == ' . $parsed_size . ' bytes'
- );
- }
-
- function testCommonParseSizeFormatSize() {
- foreach ($this->exact_test_cases as $size) {
- $this->assertEqual(
- $size,
- ($parsed_size = parse_size($string = format_size($size, NULL))),
- $size . ' == ' . $parsed_size . ' (' . $string . ')'
- );
- }
- }
- }
- class DrupalTagsHandlingTestCase extends DrupalUnitTestCase {
- var $validTags = array(
- 'Drupal' => 'Drupal',
- 'Drupal with some spaces' => 'Drupal with some spaces',
- '"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"',
- '"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!',
- );
- public static function getInfo() {
- return array(
- 'name' => 'Drupal tags handling',
- 'description' => "Performs tests on Drupal's handling of tags, both explosion and implosion tactics used.",
- 'group' => 'System'
- );
- }
-
- function testDrupalExplodeTags() {
- $string = implode(', ', array_keys($this->validTags));
- $tags = drupal_explode_tags($string);
- $this->assertTags($tags);
- }
-
- function testDrupalImplodeTags() {
- $tags = array_values($this->validTags);
-
- for ($i = 0; $i < 10; $i++) {
- $string = drupal_implode_tags($tags);
- $tags = drupal_explode_tags($string);
- }
- $this->assertTags($tags);
- }
-
- function assertTags($tags) {
- $original = $this->validTags;
- foreach ($tags as $tag) {
- $key = array_search($tag, $original);
- $this->assertTrue($key, format_string('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key)));
- unset($original[$key]);
- }
- foreach ($original as $leftover) {
- $this->fail(format_string('Leftover tag %leftover was left over.', array('%leftover' => $leftover)));
- }
- }
- }
- class CascadingStylesheetsTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Cascading stylesheets',
- 'description' => 'Tests adding various cascading stylesheets to the page.',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('php', 'locale', 'common_test');
-
- drupal_static_reset('drupal_add_css');
- }
-
- function testDefault() {
- $this->assertEqual(array(), drupal_add_css(), 'Default CSS is empty.');
- }
-
- function testModuleInfo() {
- $this->drupalGet('');
-
- $elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
- ':media' => 'all',
- ':filename' => 'tests/common_test.css',
- ));
- $this->assertTrue(count($elements), "Stylesheet with media 'all' in module .info file found.");
-
- $elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
- ':media' => 'print',
- ':filename' => 'tests/common_test.print.css',
- ));
- $this->assertTrue(count($elements), "Stylesheet with media 'print' in module .info file found.");
- }
-
- function testAddFile() {
- $path = drupal_get_path('module', 'simpletest') . '/simpletest.css';
- $css = drupal_add_css($path);
- $this->assertEqual($css[$path]['data'], $path, 'Adding a CSS file caches it properly.');
- }
-
- function testAddExternal() {
- $path = 'http://example.com/style.css';
- $css = drupal_add_css($path, 'external');
- $this->assertEqual($css[$path]['type'], 'external', 'Adding an external CSS file caches it properly.');
- }
-
- function testReset() {
- drupal_static_reset('drupal_add_css');
- $this->assertEqual(array(), drupal_add_css(), 'Resetting the CSS empties the cache.');
- }
-
- function testRenderFile() {
- $css = drupal_get_path('module', 'simpletest') . '/simpletest.css';
- drupal_add_css($css);
- $styles = drupal_get_css();
- $this->assertTrue(strpos($styles, $css) > 0, 'Rendered CSS includes the added stylesheet.');
-
- $query_string = variable_get('css_js_query_string', '0');
- $css_processed = "<style type=\"text/css\" media=\"all\">\n@import url(\"" . check_plain(file_create_url($css)) . "?" . $query_string ."\");\n</style>";
- $this->assertEqual(trim($styles), $css_processed, 'Rendered CSS includes newlines inside style tags for JavaScript use.');
- }
-
- function testRenderExternal() {
- $css = 'http://example.com/style.css';
- drupal_add_css($css, 'external');
- $styles = drupal_get_css();
-
-
- $this->assertTrue(strpos($styles, 'href="' . $css) > 0 || strpos($styles, '@import url("' . $css . '")') > 0, 'Rendering an external CSS file.');
- }
-
- function testRenderInlinePreprocess() {
- $css = 'body { padding: 0px; }';
- $css_preprocessed = '<style type="text/css" media="all">' . "\n<!--/*--><![CDATA[/*><!--*/\n" . drupal_load_stylesheet_content($css, TRUE) . "\n/*]]>*/-->\n" . '</style>';
- drupal_add_css($css, array('type' => 'inline'));
- $styles = drupal_get_css();
- $this->assertEqual(trim($styles), $css_preprocessed, 'Rendering preprocessed inline CSS adds it to the page.');
- }
-
- function testRenderRemoveCharsetPreprocess() {
- $cases = array(
- array(
- 'asset' => '@charset "UTF-8";html{font-family:"sans-serif";}',
- 'expected' => 'html{font-family:"sans-serif";}',
- ),
-
- array(
- 'asset' => "@charset 'UTF-8';\nhtml{font-family:'sans-serif';}",
- 'expected' => "\nhtml{font-family:'sans-serif';}",
- ),
- );
- foreach ($cases as $case) {
- $this->assertEqual(
- $case['expected'],
- drupal_load_stylesheet_content($case['asset']),
- 'CSS optimizing correctly removes the charset declaration.'
- );
- }
- }
-
- function testRenderInlineNoPreprocess() {
- $css = 'body { padding: 0px; }';
- drupal_add_css($css, array('type' => 'inline', 'preprocess' => FALSE));
- $styles = drupal_get_css();
- $this->assertTrue(strpos($styles, $css) > 0, 'Rendering non-preprocessed inline CSS adds it to the page.');
- }
-
- function testRenderInlineFullPage() {
- $css = 'body { font-size: 254px; }';
-
-
- $expected = 'body{font-size:254px;}';
-
- $php_format_id = 'php_code';
- $settings = array(
- 'type' => 'page',
- 'body' => array(
- LANGUAGE_NONE => array(
- array(
- 'value' => t('This tests the inline CSS!') . "<?php drupal_add_css('$css', 'inline'); ?>",
- 'format' => $php_format_id,
- ),
- ),
- ),
- 'promote' => 1,
- );
- $node = $this->drupalCreateNode($settings);
-
- $this->drupalGet('node/' . $node->nid);
- $this->assertRaw($expected, 'Inline stylesheets appear in the full page rendering.');
- }
-
- function testRenderOrder() {
-
- drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
-
- $system_path = drupal_get_path('module', 'system');
- drupal_add_css($system_path . '/system.menus.css', array('group' => CSS_SYSTEM));
- drupal_add_css($system_path . '/system.base.css', array('group' => CSS_SYSTEM, 'weight' => -10));
- drupal_add_css($system_path . '/system.theme.css', array('group' => CSS_SYSTEM));
- $expected = array(
- $system_path . '/system.base.css',
- $system_path . '/system.menus.css',
- $system_path . '/system.theme.css',
- drupal_get_path('module', 'simpletest') . '/simpletest.css',
- );
- $styles = drupal_get_css();
-
-
- if (preg_match_all('/(href="|url\(")' . preg_quote($GLOBALS['base_url'] . '/', '/') . '([^?]+)\?/', $styles, $matches)) {
- $result = $matches[2];
- }
- else {
- $result = array();
- }
- $this->assertIdentical($result, $expected, 'The CSS files are in the expected order.');
- }
-
- function testRenderOverride() {
- $system = drupal_get_path('module', 'system');
- $simpletest = drupal_get_path('module', 'simpletest');
- drupal_add_css($system . '/system.base.css');
- drupal_add_css($simpletest . '/tests/system.base.css');
-
- $styles = drupal_get_css();
- $this->assert(strpos($styles, $simpletest . '/tests/system.base.css') !== FALSE, 'The overriding CSS file is output.');
- $this->assert(strpos($styles, $system . '/system.base.css') === FALSE, 'The overridden CSS file is not output.');
- drupal_add_css($simpletest . '/tests/system.base.css');
- drupal_add_css($system . '/system.base.css');
-
- $styles = drupal_get_css();
- $this->assert(strpos($styles, $system . '/system.base.css') !== FALSE, 'The overriding CSS file is output.');
- $this->assert(strpos($styles, $simpletest . '/tests/system.base.css') === FALSE, 'The overridden CSS file is not output.');
- }
-
- function testAlter() {
-
- global $language;
- $language->direction = LANGUAGE_RTL;
- $path = drupal_get_path('module', 'system');
- drupal_add_css($path . '/system.base.css');
-
- $styles = drupal_get_css();
- $this->assert(strpos($styles, $path . '/system.base-rtl.css') !== FALSE, 'CSS is alterable as right to left overrides are added.');
-
- $language->direction = LANGUAGE_LTR;
- }
-
- function testAddCssFileWithQueryString() {
- $this->drupalGet('common-test/query-string');
- $query_string = variable_get('css_js_query_string', '0');
- $this->assertRaw(drupal_get_path('module', 'node') . '/node.css?' . $query_string, 'Query string was appended correctly to css.');
- $this->assertRaw(drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2', 'Query string not escaped on a URI.');
- }
- }
- class DrupalHTMLIdentifierTestCase extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'HTML identifiers',
- 'description' => 'Test the functions drupal_html_class(), drupal_html_id() and drupal_clean_css_identifier() for expected behavior',
- 'group' => 'System',
- );
- }
-
- function testDrupalCleanCSSIdentifier() {
-
- $identifier = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789';
- $this->assertIdentical(drupal_clean_css_identifier($identifier, array()), $identifier, 'Verify valid ASCII characters pass through.');
-
- $identifier = '¡¢£¤¥';
- $this->assertIdentical(drupal_clean_css_identifier($identifier, array()), $identifier, 'Verify valid UTF-8 characters pass through.');
-
- $this->assertIdentical(drupal_clean_css_identifier('invalid !"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ identifier', array()), 'invalididentifier', 'Strip invalid characters.');
- }
-
- function testDrupalHTMLClass() {
-
- $this->assertIdentical(drupal_html_class('CLASS NAME_[Ü]'), 'class-name--ü', 'Enforce Drupal coding standards.');
- }
-
- function testDrupalHTMLId() {
-
- $id = 'abcdefghijklmnopqrstuvwxyz-0123456789';
- $this->assertIdentical(drupal_html_id($id), $id, 'Verify valid characters pass through.');
-
- $this->assertIdentical(drupal_html_id('invalid,./:@\\^`{Üidentifier'), 'invalididentifier', 'Strip invalid characters.');
-
- $this->assertIdentical(drupal_html_id('ID NAME_[1]'), 'id-name-1', 'Enforce Drupal coding standards.');
-
- drupal_static_reset('drupal_html_id');
-
- $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id', 'Test the uniqueness of IDs #1.');
- $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id--2', 'Test the uniqueness of IDs #2.');
- $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id--3', 'Test the uniqueness of IDs #3.');
- }
- }
- class CascadingStylesheetsUnitTest extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'CSS Unit Tests',
- 'description' => 'Unit tests on CSS functions like aggregation.',
- 'group' => 'System',
- );
- }
-
- function testLoadCssBasic() {
-
-
-
-
- $testfiles = array(
- 'css_input_without_import.css',
- 'css_input_with_import.css',
- 'css_subfolder/css_input_with_import.css',
- 'comment_hacks.css'
- );
- $path = drupal_get_path('module', 'simpletest') . '/files/css_test_files';
- foreach ($testfiles as $file) {
- $file_path = $path . '/' . $file;
- $file_url = $GLOBALS['base_url'] . '/' . $file_path;
- $expected = file_get_contents($file_path . '.unoptimized.css');
- $unoptimized_output = drupal_load_stylesheet($file_path, FALSE);
- $this->assertEqual($unoptimized_output, $expected, format_string('Unoptimized CSS file has expected contents (@file)', array('@file' => $file)));
- $expected = file_get_contents($file_path . '.optimized.css');
- $optimized_output = drupal_load_stylesheet($file_path, TRUE);
- $this->assertEqual($optimized_output, $expected, format_string('Optimized CSS file has expected contents (@file)', array('@file' => $file)));
-
- $expected = file_get_contents($file_path . '.unoptimized.css');
- $unoptimized_output_url = drupal_load_stylesheet($file_url, FALSE);
- $this->assertEqual($unoptimized_output_url, $expected, format_string('Unoptimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file)));
- $expected = file_get_contents($file_path . '.optimized.css');
- $optimized_output_url = drupal_load_stylesheet($file_url, TRUE);
- $this->assertEqual($optimized_output_url, $expected, format_string('Optimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file)));
- }
- }
- }
- class DrupalHTTPRequestTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Drupal HTTP request',
- 'description' => "Performs tests on Drupal's HTTP request mechanism.",
- 'group' => 'System'
- );
- }
- function setUp() {
- parent::setUp('system_test', 'locale');
- }
- function testDrupalHTTPRequest() {
- global $is_https;
-
- $missing_scheme = drupal_http_request('example.com/path');
- $this->assertEqual($missing_scheme->code, -1002, 'Returned with "-1002" error code.');
- $this->assertEqual($missing_scheme->error, 'missing schema', 'Returned with "missing schema" error message.');
- $unable_to_parse = drupal_http_request('http:///path');
- $this->assertEqual($unable_to_parse->code, -1001, 'Returned with "-1001" error code.');
- $this->assertEqual($unable_to_parse->error, 'unable to parse URL', 'Returned with "unable to parse URL" error message.');
-
- $result = drupal_http_request(url('node', array('absolute' => TRUE)));
- $this->assertEqual($result->code, 200, 'Fetched page successfully.');
- $this->drupalSetContent($result->data);
- $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), 'Site title matches.');
-
- $result = drupal_http_request(url('pagedoesnotexist', array('absolute' => TRUE)));
- $this->assertTrue(!empty($result->protocol), 'Result protocol is returned.');
- $this->assertEqual($result->code, '404', 'Result code is 404');
- $this->assertEqual($result->status_message, 'Not Found', 'Result status message is "Not Found"');
-
-
-
- if (!$is_https) {
-
-
-
-
-
-
- timer_start(__METHOD__);
- $result = drupal_http_request(url('system-test/sleep/10', array('absolute' => TRUE)), array('timeout' => 2));
- $time = timer_read(__METHOD__) / 1000;
- $this->assertTrue(1.8 < $time && $time < 5, format_string('Request timed out (%time seconds).', array('%time' => $time)));
- $this->assertTrue($result->error, 'An error message was returned.');
- $this->assertEqual($result->code, HTTP_REQUEST_TIMEOUT, 'Proper error code was returned.');
- }
- }
- function testDrupalHTTPRequestBasicAuth() {
- $username = $this->randomName();
- $password = $this->randomName();
- $url = url('system-test/auth', array('absolute' => TRUE));
- $auth = str_replace('://', '://' . $username . ':' . $password . '@', $url);
- $result = drupal_http_request($auth);
- $this->drupalSetContent($result->data);
- $this->assertRaw($username, 'Username is passed correctly.');
- $this->assertRaw($password, 'Password is passed correctly.');
- }
- function testDrupalHTTPRequestRedirect() {
- $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_301->redirect_code, 301, 'drupal_http_request follows the 301 redirect.');
- $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 0));
- $this->assertFalse(isset($redirect_301->redirect_code), 'drupal_http_request does not follow 301 redirect if max_redirects = 0.');
- $redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_invalid->code, -1002, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
- $this->assertEqual($redirect_invalid->error, 'missing schema', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
- $redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_invalid->code, -1001, format_string('301 redirect to invalid URL returned with error message code "!error".', array('!error' => $redirect_invalid->error)));
- $this->assertEqual($redirect_invalid->error, 'unable to parse URL', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
- $redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_invalid->code, -1003, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
- $this->assertEqual($redirect_invalid->error, 'invalid schema ftp', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
- $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_302->redirect_code, 302, 'drupal_http_request follows the 302 redirect.');
- $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 0));
- $this->assertFalse(isset($redirect_302->redirect_code), 'drupal_http_request does not follow 302 redirect if $retry = 0.');
- $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($redirect_307->redirect_code, 307, 'drupal_http_request follows the 307 redirect.');
- $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 0));
- $this->assertFalse(isset($redirect_307->redirect_code), 'drupal_http_request does not follow 307 redirect if max_redirects = 0.');
- $multiple_redirect_final_url = url('system-test/multiple-redirects/0', array('absolute' => TRUE));
- $multiple_redirect_1 = drupal_http_request(url('system-test/multiple-redirects/1', array('absolute' => TRUE)), array('max_redirects' => 1));
- $this->assertEqual($multiple_redirect_1->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 1 redirect.');
- $multiple_redirect_3 = drupal_http_request(url('system-test/multiple-redirects/3', array('absolute' => TRUE)), array('max_redirects' => 3));
- $this->assertEqual($multiple_redirect_3->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 3 redirects.');
- }
-
- function testDrupalHTTPRequestHeaders() {
-
- $request = drupal_http_request(url('<front>', array('absolute' => TRUE)));
- $this->assertEqual($request->headers['content-language'], 'en', 'Content-Language HTTP header is English.');
-
- locale_add_language('de', 'German', 'Deutsch', LANGUAGE_LTR, '', '', TRUE, TRUE);
-
- $request = drupal_http_request(url('<front>', array('absolute' => TRUE)));
- $this->assertEqual($request->headers['content-language'], 'de', 'Content-Language HTTP header is German.');
- }
- }
- class DrupalHTTPResponseStatusLineTest extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Drupal HTTP request response status parsing',
- 'description' => 'Perform unit tests on _drupal_parse_response_status().',
- 'group' => 'System',
- );
- }
-
- public function testStatusLine() {
-
- $data = $this->statusLineData();
- foreach($data as $test_case) {
- $test_data = array_shift($test_case);
- $expected = array_shift($test_case);
- $outcome = _drupal_parse_response_status($test_data);
- foreach(array_keys($expected) as $key) {
- $this->assertIdentical($outcome[$key], $expected[$key]);
- }
- }
- }
-
- protected function statusLineData() {
- return array(
- array(
- 'HTTP/1.1 200 OK',
- array(
- 'http_version' => 'HTTP/1.1',
- 'response_code' => '200',
- 'reason_phrase' => 'OK',
- ),
- ),
-
- array(
- 'HTTP/1.1 200',
- array(
- 'http_version' => 'HTTP/1.1',
- 'response_code' => '200',
- 'reason_phrase' => '',
- ),
- ),
-
- array(
- 'version code multi word explanation',
- array(
- 'http_version' => 'version',
- 'response_code' => 'code',
- 'reason_phrase' => 'multi word explanation',
- ),
- ),
- );
- }
- }
- class DrupalSetContentTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Drupal set/get regions',
- 'description' => 'Performs tests on setting and retrieiving content from theme regions.',
- 'group' => 'System'
- );
- }
-
- function testRegions() {
- global $theme_key;
- $block_regions = array_keys(system_region_list($theme_key));
- $delimiter = $this->randomName(32);
- $values = array();
-
- foreach ($block_regions as $region) {
- $first_chunk = $this->randomName(32);
- drupal_add_region_content($region, $first_chunk);
- $second_chunk = $this->randomName(32);
- drupal_add_region_content($region, $second_chunk);
-
- $values[$region] = $first_chunk . $delimiter . $second_chunk;
- }
-
- $content = drupal_get_region_content(NULL, $delimiter);
- foreach ($content as $region => $region_content) {
- $this->assertEqual($region_content, $values[$region], format_string('@region region text verified when fetching all regions', array('@region' => $region)));
- }
-
- foreach ($block_regions as $region) {
- $region_content = drupal_get_region_content($region, $delimiter);
- $this->assertEqual($region_content, $values[$region], format_string('@region region text verified when fetching single region.', array('@region' => $region)));
- }
- }
- }
- class DrupalGotoTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Drupal goto',
- 'description' => 'Performs tests on the drupal_goto function and hook_drupal_goto_alter',
- 'group' => 'System'
- );
- }
- function setUp() {
- parent::setUp('common_test');
- }
-
- function testDrupalGoto() {
- $this->drupalGet('common-test/drupal_goto/redirect');
- $headers = $this->drupalGetHeaders(TRUE);
- list(, $status) = explode(' ', $headers[0][':status'], 3);
- $this->assertEqual($status, 302, 'Expected response code was sent.');
- $this->assertText('drupal_goto', 'Drupal goto redirect succeeded.');
- $this->assertEqual($this->getUrl(), url('common-test/drupal_goto', array('absolute' => TRUE)), 'Drupal goto redirected to expected URL.');
- $this->drupalGet('common-test/drupal_goto/redirect_advanced');
- $headers = $this->drupalGetHeaders(TRUE);
- list(, $status) = explode(' ', $headers[0][':status'], 3);
- $this->assertEqual($status, 301, 'Expected response code was sent.');
- $this->assertText('drupal_goto', 'Drupal goto redirect succeeded.');
- $this->assertEqual($this->getUrl(), url('common-test/drupal_goto', array('query' => array('foo' => '123'), 'absolute' => TRUE)), 'Drupal goto redirected to expected URL.');
-
-
- $destination = 'common-test/drupal_goto/destination?foo=%2525&bar=123';
- $this->drupalGet('common-test/drupal_goto/redirect', array('query' => array('destination' => $destination)));
- $this->assertText('drupal_goto', 'Drupal goto redirect with destination succeeded.');
- $this->assertEqual($this->getUrl(), url('common-test/drupal_goto/destination', array('query' => array('foo' => '%25', 'bar' => '123'), 'absolute' => TRUE)), 'Drupal goto redirected to given query string destination.');
- }
-
- function testDrupalGotoAlter() {
- $this->drupalGet('common-test/drupal_goto/redirect_fail');
- $this->assertNoText(t("Drupal goto failed to stop program"), "Drupal goto stopped program.");
- $this->assertNoText('drupal_goto_fail', "Drupal goto redirect failed.");
- }
-
- function testDrupalGetDestination() {
- $query = $this->randomName(10);
-
- $this->drupalGet('common-test/destination', array('query' => array('destination' => $query)));
- $this->assertText('The destination: ' . $query, 'The given query string destination is determined as destination.');
-
- $this->drupalGet('common-test/destination', array('query' => array($query => NULL)));
- $url = 'common-test/destination?' . $query;
- $this->assertText('The destination: ' . $url, 'The current path is determined as destination.');
- }
- }
- class JavaScriptTestCase extends DrupalWebTestCase {
-
- protected $preprocess_js = NULL;
- public static function getInfo() {
- return array(
- 'name' => 'JavaScript',
- 'description' => 'Tests the JavaScript system.',
- 'group' => 'System'
- );
- }
- function setUp() {
-
- parent::setUp('locale', 'simpletest', 'common_test');
-
- $this->preprocess_js = variable_get('preprocess_js', 0);
- variable_set('preprocess_js', 0);
-
- drupal_static_reset('drupal_add_js');
- drupal_static_reset('drupal_add_library');
- }
- function tearDown() {
-
- variable_set('preprocess_js', $this->preprocess_js);
- parent::tearDown();
- }
-
- function testDefault() {
- $this->assertEqual(array(), drupal_add_js(), 'Default JavaScript is empty.');
- }
-
- function testAddFile() {
- $javascript = drupal_add_js('misc/collapse.js');
- $this->assertTrue(array_key_exists('misc/jquery.js', $javascript), 'jQuery is added when a file is added.');
- $this->assertTrue(array_key_exists('misc/drupal.js', $javascript), 'Drupal.js is added when file is added.');
- $this->assertTrue(array_key_exists('misc/collapse.js', $javascript), 'JavaScript files are correctly added.');
- $this->assertEqual(base_path(), $javascript['settings']['data'][0]['basePath'], 'Base path JavaScript setting is correctly set.');
- url('', array('prefix' => &$prefix));
- $this->assertEqual(empty($prefix) ? '' : $prefix, $javascript['settings']['data'][1]['pathPrefix'], 'Path prefix JavaScript setting is correctly set.');
- }
-
- function testAddSetting() {
- $javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
- $this->assertEqual(280342800, $javascript['settings']['data'][2]['dries'], 'JavaScript setting is set correctly.');
- $this->assertEqual('rocks', $javascript['settings']['data'][2]['drupal'], 'The other JavaScript setting is set correctly.');
- }
-
- function testAddExternal() {
- $path = 'http://example.com/script.js';
- $javascript = drupal_add_js($path, 'external');
- $this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), 'Added an external JavaScript file.');
- }
-
- function testHeaderSetting() {
-
- drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting');
- drupal_add_js(array('commonTest' => 'commonTestShouldAppear'), 'setting');
-
- drupal_add_js(array('commonTestArray' => array('commonTestValue0')), 'setting');
- drupal_add_js(array('commonTestArray' => array('commonTestValue1')), 'setting');
- drupal_add_js(array('commonTestArray' => array('commonTestValue2')), 'setting');
-
- drupal_add_js(array('commonTestArray' => array('key' => 'commonTestOldValue')), 'setting');
- drupal_add_js(array('commonTestArray' => array('key' => 'commonTestNewValue')), 'setting');
- $javascript = drupal_get_js('header');
- $this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') > 0, 'Rendered JavaScript header includes jQuery.');
- $this->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.');
-
- $this->assertTrue(strpos($javascript, 'commonTestShouldAppear') > 0, 'Rendered JavaScript header returns custom setting.');
- $this->assertTrue(strpos($javascript, 'commonTestShouldNotAppear') === FALSE, 'drupal_add_js() correctly overrides a custom setting.');
-
-
- $array_values_appear = strpos($javascript, 'commonTestValue0') > 0 && strpos($javascript, 'commonTestValue1') > 0 && strpos($javascript, 'commonTestValue2') > 0;
- $this->assertTrue($array_values_appear, 'drupal_add_js() correctly adds settings to the end of an indexed array.');
-
-
- $associative_array_override = strpos($javascript, 'commonTestNewValue') > 0 && strpos($javascript, 'commonTestOldValue') === FALSE;
- $this->assertTrue($associative_array_override, 'drupal_add_js() correctly overrides settings within an associative array.');
- }
-
- function testReset() {
- drupal_add_js('misc/collapse.js');
- drupal_static_reset('drupal_add_js');
- $this->assertEqual(array(), drupal_add_js(), 'Resetting the JavaScript correctly empties the cache.');
- }
-
- function testAddInline() {
- $inline = 'jQuery(function () { });';
- $javascript = drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
- $this->assertTrue(array_key_exists('misc/jquery.js', $javascript), 'jQuery is added when inline scripts are added.');
- $data = end($javascript);
- $this->assertEqual($inline, $data['data'], 'Inline JavaScript is correctly added to the footer.');
- }
-
- function testRenderExternal() {
- $external = 'http://example.com/example.js';
- drupal_add_js($external, 'external');
- $javascript = drupal_get_js();
-
- $this->assertTrue(strpos($javascript, 'src="' . $external) > 0, 'Rendering an external JavaScript file.');
- }
-
- function testFooterHTML() {
- $inline = 'jQuery(function () { });';
- drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
- $javascript = drupal_get_js('footer');
- $this->assertTrue(strpos($javascript, $inline) > 0, 'Rendered JavaScript footer returns the inline code.');
- }
-
- function testJavaScriptAlwaysUseJQuery() {
-
-
- $this->drupalGet('');
- $this->assertRaw('misc/jquery.js', 'Default behavior: The front page of the site includes jquery.js.');
- $this->assertRaw('misc/drupal.js', 'Default behavior: The front page of the site includes drupal.js.');
- $this->assertRaw('Drupal.settings', 'Default behavior: The front page of the site includes Drupal settings.');
- $this->assertRaw('basePath', 'Default behavior: The front page of the site includes the basePath Drupal setting.');
-
-
-
- variable_set('javascript_always_use_jquery', FALSE);
- $this->drupalGet('');
- $this->assertNoRaw('misc/jquery.js', 'When "javascript_always_use_jquery" is FALSE: The front page of the site does not include jquery.js.');
- $this->assertNoRaw('misc/drupal.js', 'When "javascript_always_use_jquery" is FALSE: The front page of the site does not include drupal.js.');
- $this->assertNoRaw('Drupal.settings', 'When "javascript_always_use_jquery" is FALSE: The front page of the site does not include Drupal settings.');
- $this->assertNoRaw('basePath', 'When "javascript_always_use_jquery" is FALSE: The front page of the site does not include the basePath Drupal setting.');
- variable_del('javascript_always_use_jquery');
-
-
- $this->resetStaticVariables();
- drupal_add_js(array('testJavaScriptSetting' => 'test'), 'setting');
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when only settings have been added includes jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when only settings have been added includes drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when only settings have been added includes Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when only settings have been added includes the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'testJavaScriptSetting') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when only settings have been added includes the added Drupal settings.');
-
-
-
-
-
- $this->resetStaticVariables();
- variable_set('javascript_always_use_jquery', FALSE);
- drupal_add_js(array('testJavaScriptSetting' => 'test'), 'setting');
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when only settings have been added does not include jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when only settings have been added does not include drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when only settings have been added does not include Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when only settings have been added does not include the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'testJavaScriptSetting') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when only settings have been added does not include the added Drupal settings.');
- variable_del('javascript_always_use_jquery');
-
-
- $this->resetStaticVariables();
- drupal_add_js('misc/collapse.js');
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes the custom file.');
-
-
-
-
- $this->resetStaticVariables();
- variable_set('javascript_always_use_jquery', FALSE);
- drupal_add_js('misc/collapse.js');
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file has been added includes the custom file.');
- variable_del('javascript_always_use_jquery');
-
-
-
- $this->resetStaticVariables();
- drupal_add_js('misc/collapse.js', array('requires_jquery' => FALSE));
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') !== FALSE, 'Default behavior: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes the custom file.');
-
-
-
-
- $this->resetStaticVariables();
- variable_set('javascript_always_use_jquery', FALSE);
- drupal_add_js('misc/collapse.js', array('requires_jquery' => FALSE));
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added does not include jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added does not include drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added does not include Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') === FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added does not include the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when a custom JavaScript file that does not require jQuery has been added includes the custom file.');
- variable_del('javascript_always_use_jquery');
-
-
-
-
- $this->resetStaticVariables();
- variable_set('javascript_always_use_jquery', FALSE);
- drupal_add_js('misc/collapse.js', array('requires_jquery' => FALSE));
- drupal_add_js('misc/ajax.js');
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/jquery.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes jquery.js.');
- $this->assertTrue(strpos($javascript, 'misc/drupal.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes drupal.js.');
- $this->assertTrue(strpos($javascript, 'Drupal.settings') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes Drupal.settings.');
- $this->assertTrue(strpos($javascript, 'basePath') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes the basePath Drupal setting.');
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes the first custom file.');
- $this->assertTrue(strpos($javascript, 'misc/ajax.js') !== FALSE, 'When "javascript_always_use_jquery" is FALSE: The JavaScript returned by drupal_get_js() when at least one custom JavaScript file that requires jQuery has been added includes the second custom file.');
- variable_del('javascript_always_use_jquery');
- }
-
- function testNoCache() {
- $javascript = drupal_add_js('misc/collapse.js', array('cache' => FALSE));
- $this->assertFalse($javascript['misc/collapse.js']['preprocess'], 'Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.');
- }
-
- function testDifferentGroup() {
- $javascript = drupal_add_js('misc/collapse.js', array('group' => JS_THEME));
- $this->assertEqual($javascript['misc/collapse.js']['group'], JS_THEME, 'Adding a JavaScript file with a different group caches the given group.');
- }
-
- function testDifferentWeight() {
- $javascript = drupal_add_js('misc/collapse.js', array('weight' => 2));
- $this->assertEqual($javascript['misc/collapse.js']['weight'], 2, 'Adding a JavaScript file with a different weight caches the given weight.');
- }
-
- function testAggregationOrder() {
-
- variable_set('preprocess_js', 1);
- drupal_static_reset('drupal_add_js');
-
- drupal_add_js('misc/ajax.js');
- drupal_add_js('misc/autocomplete.js');
- $js_items = drupal_add_js();
- drupal_build_js_cache(array(
- 'misc/ajax.js' => $js_items['misc/ajax.js'],
- 'misc/autocomplete.js' => $js_items['misc/autocomplete.js']
- ));
-
- $cache = array_keys(variable_get('drupal_js_cache_files', array()));
- $expected_key = $cache[0];
-
- variable_del('drupal_js_cache_files');
- drupal_static_reset('drupal_add_js');
- drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer'));
- drupal_add_js('misc/ajax.js');
- drupal_add_js('misc/autocomplete.js');
-
- $js_items = drupal_add_js();
- drupal_build_js_cache(array(
- 'misc/ajax.js' => $js_items['misc/ajax.js'],
- 'misc/autocomplete.js' => $js_items['misc/autocomplete.js']
- ));
-
- $cache = array_keys(variable_get('drupal_js_cache_files', array()));
- $key = $cache[0];
- $this->assertEqual($key, $expected_key, 'JavaScript aggregation is not affected by ordering in different scopes.');
- }
-
- function testRenderOrder() {
-
- drupal_add_js('(function($){alert("Weight 5 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
- drupal_add_js('(function($){alert("Weight 0 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
- drupal_add_js('(function($){alert("Weight 0 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
- drupal_add_js('(function($){alert("Weight -8 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
- drupal_add_js('(function($){alert("Weight -8 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
- drupal_add_js('(function($){alert("Weight -8 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
- drupal_add_js('http://example.com/example.js?Weight -5 #1', array('type' => 'external', 'scope' => 'footer', 'weight' => -5));
- drupal_add_js('(function($){alert("Weight -8 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
- drupal_add_js('(function($){alert("Weight 5 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
- drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
-
- $expected = array(
- "-8 #1",
- "-8 #2",
- "-8 #3",
- "-8 #4",
- "-5 #1",
- "0 #1",
- "0 #2",
- "0 #3",
- "5 #1",
- "5 #2",
- );
-
- $js = drupal_get_js('footer');
- $matches = array();
- if (preg_match_all('/Weight\s([-0-9]+\s[#0-9]+)/', $js, $matches)) {
- $result = $matches[1];
- }
- else {
- $result = array();
- }
- $this->assertIdentical($result, $expected, 'JavaScript is added in the expected weight order.');
- }
-
- function testRenderDifferentWeight() {
-
-
-
- drupal_add_js('misc/collapse.js', array('group' => JS_LIBRARY, 'every_page' => TRUE, 'weight' => -21));
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'misc/collapse.js') < strpos($javascript, 'misc/jquery.js'), 'Rendering a JavaScript file above jQuery.');
- }
-
- function testAlter() {
-
- drupal_add_js('misc/tableselect.js');
- drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => 9999));
-
-
-
- $javascript = drupal_get_js();
- $this->assertTrue(strpos($javascript, 'simpletest.js') < strpos($javascript, 'misc/tableselect.js'), 'Altering JavaScript weight through the alter hook.');
- }
-
- function testLibraryRender() {
- $result = drupal_add_library('system', 'farbtastic');
- $this->assertTrue($result !== FALSE, 'Library was added without errors.');
- $scripts = drupal_get_js();
- $styles = drupal_get_css();
- $this->assertTrue(strpos($scripts, 'misc/farbtastic/farbtastic.js'), 'JavaScript of library was added to the page.');
- $this->assertTrue(strpos($styles, 'misc/farbtastic/farbtastic.css'), 'Stylesheet of library was added to the page.');
- }
-
- function testLibraryAlter() {
-
- $library = drupal_get_library('system', 'farbtastic');
- $this->assertEqual($library['title'], 'Farbtastic: Altered Library', 'Registered libraries were altered.');
-
- drupal_add_library('system', 'farbtastic');
- $scripts = drupal_get_js();
- $this->assertTrue(strpos($scripts, 'misc/jquery.form.js'), 'Altered library dependencies are added to the page.');
- }
-
- function testLibraryNameConflicts() {
- $farbtastic = drupal_get_library('common_test', 'farbtastic');
- $this->assertEqual($farbtastic['title'], 'Custom Farbtastic Library', 'Alternative libraries can be added to the page.');
- }
-
- function testLibraryUnknown() {
- $result = drupal_get_library('unknown', 'unknown');
- $this->assertFalse($result, 'Unknown library returned FALSE.');
- drupal_static_reset('drupal_get_library');
- $result = drupal_add_library('unknown', 'unknown');
- $this->assertFalse($result, 'Unknown library returned FALSE.');
- $scripts = drupal_get_js();
- $this->assertTrue(strpos($scripts, 'unknown') === FALSE, 'Unknown library was not added to the page.');
- }
-
- function testAttachedLibrary() {
- $element['#attached']['library'][] = array('system', 'farbtastic');
- drupal_render($element);
- $scripts = drupal_get_js();
- $this->assertTrue(strpos($scripts, 'misc/farbtastic/farbtastic.js'), 'The attached_library property adds the additional libraries.');
- }
-
- function testGetLibrary() {
-
- $libraries = drupal_get_library('common_test');
- $this->assertTrue(isset($libraries['farbtastic']), 'Retrieved all module libraries.');
-
-
- $libraries = drupal_get_library('locale');
- $this->assertEqual($libraries, array(), 'Retrieving libraries from a module not implementing hook_library() returns an emtpy array.');
-
- $farbtastic = drupal_get_library('common_test', 'farbtastic');
- $this->assertEqual($farbtastic['version'], '5.3', 'Retrieved a single library.');
-
- $farbtastic = drupal_get_library('common_test', 'foo');
- $this->assertIdentical($farbtastic, FALSE, 'Retrieving a non-existing library returns FALSE.');
- }
-
- function testAddJsFileWithQueryString() {
- $this->drupalGet('common-test/query-string');
- $query_string = variable_get('css_js_query_string', '0');
- $this->assertRaw(drupal_get_path('module', 'node') . '/node.js?' . $query_string, 'Query string was appended correctly to js.');
- }
-
- function resetStaticVariables() {
- drupal_static_reset('drupal_add_js');
- drupal_static_reset('drupal_add_library');
- drupal_static_reset('drupal_get_library');
- }
- }
- class DrupalRenderTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'drupal_render()',
- 'description' => 'Performs functional tests on drupal_render().',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('common_test');
- }
-
- function testDrupalRenderBasics() {
- $types = array(
- array(
- 'name' => 'null',
- 'value' => NULL,
- 'expected' => '',
- ),
- array(
- 'name' => 'no value',
- 'expected' => '',
- ),
- array(
- 'name' => 'empty string',
- 'value' => '',
- 'expected' => '',
- ),
- array(
- 'name' => 'no access',
- 'value' => array(
- '#markup' => 'foo',
- '#access' => FALSE,
- ),
- 'expected' => '',
- ),
- array(
- 'name' => 'previously printed',
- 'value' => array(
- '#markup' => 'foo',
- '#printed' => TRUE,
- ),
- 'expected' => '',
- ),
- array(
- 'name' => 'printed in prerender',
- 'value' => array(
- '#markup' => 'foo',
- '#pre_render' => array('common_test_drupal_render_printing_pre_render'),
- ),
- 'expected' => '',
- ),
- array(
- 'name' => 'basic renderable array',
- 'value' => array('#markup' => 'foo'),
- 'expected' => 'foo',
- ),
- );
- foreach($types as $type) {
- $this->assertIdentical(drupal_render($type['value']), $type['expected'], '"' . $type['name'] . '" input rendered correctly by drupal_render().');
- }
- }
-
- function testDrupalRenderSorting() {
- $first = $this->randomName();
- $second = $this->randomName();
-
- $elements = array(
- 'second' => array(
- '#weight' => 10,
- '#markup' => $second,
- ),
- 'first' => array(
- '#weight' => 0,
- '#markup' => $first,
- ),
- );
- $output = drupal_render($elements);
-
- $this->assertTrue(strpos($output, $second) > strpos($output, $first), 'Elements were sorted correctly by weight.');
-
- $this->assertTrue($elements['#sorted'], "'#sorted' => TRUE was added to the array");
-
-
-
- $children = element_children($elements);
- $this->assertTrue(array_shift($children) == 'first', 'Child found in the correct order.');
- $this->assertTrue(array_shift($children) == 'second', 'Child found in the correct order.');
-
- $elements = array(
- 'second' => array(
- '#weight' => 10,
- '#markup' => $second,
- ),
- 'first' => array(
- '#weight' => 0,
- '#markup' => $first,
- ),
- '#sorted' => TRUE,
- );
- $output = drupal_render($elements);
-
- $this->assertTrue(strpos($output, $second) < strpos($output, $first), 'Elements were not sorted.');
- }
-
- function testDrupalRenderChildrenAttached() {
-
- $request_method = $_SERVER['REQUEST_METHOD'];
- $_SERVER['REQUEST_METHOD'] = 'GET';
-
-
- $parent_js = drupal_get_path('module', 'user') . '/user.js';
- $child_js = drupal_get_path('module', 'forum') . '/forum.js';
- $subchild_js = drupal_get_path('module', 'book') . '/book.js';
- $element = array(
- '#type' => 'fieldset',
- '#cache' => array(
- 'keys' => array('simpletest', 'drupal_render', 'children_attached'),
- ),
- '#attached' => array('js' => array($parent_js)),
- '#title' => 'Parent',
- );
- $element['child'] = array(
- '#type' => 'fieldset',
- '#attached' => array('js' => array($child_js)),
- '#title' => 'Child',
- );
- $element['child']['subchild'] = array(
- '#attached' => array('js' => array($subchild_js)),
- '#markup' => 'Subchild',
- );
-
- drupal_render($element);
- $scripts = drupal_get_js();
- $this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included.');
- $this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included.');
- $this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included.');
-
-
- drupal_static_reset('drupal_add_js');
- $this->assertTrue(drupal_render_cache_get($element), 'The element was retrieved from cache.');
- $scripts = drupal_get_js();
- $this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included when loading from cache.');
- $this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included when loading from cache.');
- $this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included when loading from cache.');
- $_SERVER['REQUEST_METHOD'] = $request_method;
- }
-
- function testDrupalRenderThemeArguments() {
- $element = array(
- '#theme' => 'common_test_foo',
- );
-
- $this->assertEqual(drupal_render($element), 'foobar', 'Defaults work');
- $element = array(
- '#theme' => 'common_test_foo',
- '#foo' => $this->randomName(),
- '#bar' => $this->randomName(),
- );
-
- $this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
- }
-
- function testDrupalRenderFormElements() {
-
- $element = array(
- '#type' => 'button',
- '#value' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'submit'));
- $element = array(
- '#type' => 'textfield',
- '#title' => $this->randomName(),
- '#value' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'text'));
- $element = array(
- '#type' => 'password',
- '#title' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'password'));
- $element = array(
- '#type' => 'textarea',
- '#title' => $this->randomName(),
- '#value' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//textarea');
- $element = array(
- '#type' => 'radio',
- '#title' => $this->randomName(),
- '#value' => FALSE,
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'radio'));
- $element = array(
- '#type' => 'checkbox',
- '#title' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'checkbox'));
- $element = array(
- '#type' => 'select',
- '#title' => $this->randomName(),
- '#options' => array(
- 0 => $this->randomName(),
- 1 => $this->randomName(),
- ),
- );
- $this->assertRenderedElement($element, '//select');
- $element = array(
- '#type' => 'file',
- '#title' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'file'));
- $element = array(
- '#type' => 'item',
- '#title' => $this->randomName(),
- '#markup' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', array(
- ':class' => 'form-type-item',
- ':markup' => $element['#markup'],
- ':label' => $element['#title'],
- ));
- $element = array(
- '#type' => 'hidden',
- '#title' => $this->randomName(),
- '#value' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'hidden'));
- $element = array(
- '#type' => 'link',
- '#title' => $this->randomName(),
- '#href' => $this->randomName(),
- '#options' => array(
- 'absolute' => TRUE,
- ),
- );
- $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', array(
- ':href' => url($element['#href'], array('absolute' => TRUE)),
- ':title' => $element['#title'],
- ));
- $element = array(
- '#type' => 'fieldset',
- '#title' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//fieldset/legend[contains(., :title)]', array(
- ':title' => $element['#title'],
- ));
- $element['item'] = array(
- '#type' => 'item',
- '#title' => $this->randomName(),
- '#markup' => $this->randomName(),
- );
- $this->assertRenderedElement($element, '//fieldset/div/div[contains(@class, :class) and contains(., :markup)]', array(
- ':class' => 'form-type-item',
- ':markup' => $element['item']['#markup'],
- ));
- }
- protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) {
- $original_element = $element;
- $this->drupalSetContent(drupal_render($element));
- $this->verbose('<pre>' . check_plain(var_export($original_element, TRUE)) . '</pre>'
- . '<pre>' . check_plain(var_export($element, TRUE)) . '</pre>'
- . '<hr />' . $this->drupalGetContent()
- );
-
- $xpath = $this->buildXPathQuery($xpath, $xpath_args);
- $element += array('#value' => NULL);
- $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', array(
- '@type' => var_export($element['#type'], TRUE),
- )));
- }
-
- function testDrupalRenderCache() {
-
- $request_method = $_SERVER['REQUEST_METHOD'];
- $_SERVER['REQUEST_METHOD'] = 'GET';
-
- $test_element = array(
- '#cache' => array(
- 'cid' => 'render_cache_test',
- ),
- '#markup' => '',
- );
-
-
- $element = $test_element;
- drupal_render($element);
- $this->assertTrue(isset($element['#printed']), 'No cache hit');
-
-
- $element = $test_element;
- drupal_render($element);
- $this->assertFalse(isset($element['#printed']), 'Cache hit');
-
- $_SERVER['REQUEST_METHOD'] = $request_method;
- }
- }
- class ValidUrlTestCase extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Valid URL',
- 'description' => "Performs tests on Drupal's valid URL function.",
- 'group' => 'System'
- );
- }
-
- function testValidAbsolute() {
- $url_schemes = array('http', 'https', 'ftp');
- $valid_absolute_urls = array(
- 'example.com',
- 'www.example.com',
- 'ex-ample.com',
- '3xampl3.com',
- 'example.com/paren(the)sis',
- 'example.com/index.html#pagetop',
- 'example.com:8080',
- 'subdomain.example.com',
- 'example.com/index.php?q=node',
- 'example.com/index.php?q=node¶m=false',
- 'user@www.example.com',
- 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop',
- '127.0.0.1',
- 'example.org?',
- 'john%20doe:secret:foo@example.org/',
- 'example.org/~,$\'*;',
- 'caf%C3%A9.example.org',
- '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html',
- );
- foreach ($url_schemes as $scheme) {
- foreach ($valid_absolute_urls as $url) {
- $test_url = $scheme . '://' . $url;
- $valid_url = valid_url($test_url, TRUE);
- $this->assertTrue($valid_url, format_string('@url is a valid url.', array('@url' => $test_url)));
- }
- }
- }
-
- function testInvalidAbsolute() {
- $url_schemes = array('http', 'https', 'ftp');
- $invalid_ablosule_urls = array(
- '',
- 'ex!ample.com',
- 'ex%ample.com',
- );
- foreach ($url_schemes as $scheme) {
- foreach ($invalid_ablosule_urls as $url) {
- $test_url = $scheme . '://' . $url;
- $valid_url = valid_url($test_url, TRUE);
- $this->assertFalse($valid_url, format_string('@url is NOT a valid url.', array('@url' => $test_url)));
- }
- }
- }
-
- function testValidRelative() {
- $valid_relative_urls = array(
- 'paren(the)sis',
- 'index.html#pagetop',
- 'index.php?q=node',
- 'index.php?q=node¶m=false',
- 'login.php?do=login&style=%23#pagetop',
- );
- foreach (array('', '/') as $front) {
- foreach ($valid_relative_urls as $url) {
- $test_url = $front . $url;
- $valid_url = valid_url($test_url);
- $this->assertTrue($valid_url, format_string('@url is a valid url.', array('@url' => $test_url)));
- }
- }
- }
-
- function testInvalidRelative() {
- $invalid_relative_urls = array(
- 'ex^mple',
- 'example<>',
- 'ex%ample',
- );
- foreach (array('', '/') as $front) {
- foreach ($invalid_relative_urls as $url) {
- $test_url = $front . $url;
- $valid_url = valid_url($test_url);
- $this->assertFALSE($valid_url, format_string('@url is NOT a valid url.', array('@url' => $test_url)));
- }
- }
- }
- }
- class DrupalDataApiTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Data API functions',
- 'description' => 'Tests the performance of CRUD APIs.',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('database_test');
- }
-
- function testDrupalWriteRecord() {
-
- $person = new stdClass();
- $person->name = 'John';
- $person->unknown_column = 123;
- $insert_result = drupal_write_record('test', $person);
- $this->assertTrue($insert_result == SAVED_NEW, 'Correct value returned when a record is inserted with drupal_write_record() for a table with a single-field primary key.');
- $this->assertTrue(isset($person->id), 'Primary key is set on record created with drupal_write_record().');
- $this->assertIdentical($person->age, 0, 'Age field set to default value.');
- $this->assertIdentical($person->job, 'Undefined', 'Job field set to default value.');
-
- $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'John', 'Name field set.');
- $this->assertIdentical($result->age, '0', 'Age field set to default value.');
- $this->assertIdentical($result->job, 'Undefined', 'Job field set to default value.');
- $this->assertFalse(isset($result->unknown_column), 'Unknown column was ignored.');
-
- $person->name = 'Peter';
- $person->age = 27;
- $person->job = NULL;
- $update_result = drupal_write_record('test', $person, array('id'));
- $this->assertTrue($update_result == SAVED_UPDATED, 'Correct value returned when a record updated with drupal_write_record() for table with single-field primary key.');
-
- $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Peter', 'Name field set.');
- $this->assertIdentical($result->age, '27', 'Age field set.');
- $this->assertIdentical($result->job, '', 'Job field set and cast to string.');
-
- $person = new stdClass();
- $person->name = 'Ringo';
- $person->age = NULL;
- $person->job = NULL;
- $insert_result = drupal_write_record('test', $person);
- $this->assertTrue(isset($person->id), 'Primary key is set on record created with drupal_write_record().');
- $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Ringo', 'Name field set.');
- $this->assertIdentical($result->age, '0', 'Age field set.');
- $this->assertIdentical($result->job, '', 'Job field set.');
-
- $person = new stdClass();
- $person->name = 'Paul';
- $person->age = NULL;
- $insert_result = drupal_write_record('test_null', $person);
- $this->assertTrue(isset($person->id), 'Primary key is set on record created with drupal_write_record().');
- $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Paul', 'Name field set.');
- $this->assertIdentical($result->age, NULL, 'Age field set.');
-
- $person = new stdClass();
- $person->name = 'Meredith';
- $insert_result = drupal_write_record('test_null', $person);
- $this->assertTrue(isset($person->id), 'Primary key is set on record created with drupal_write_record().');
- $this->assertIdentical($person->age, 0, 'Age field set to default value.');
- $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Meredith', 'Name field set.');
- $this->assertIdentical($result->age, '0', 'Age field set to default value.');
-
- $person->name = 'Mary';
- $person->age = NULL;
- $update_result = drupal_write_record('test_null', $person, array('id'));
- $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Mary', 'Name field set.');
- $this->assertIdentical($result->age, NULL, 'Age field set.');
-
- $person = new stdClass();
- $person->name = 'Dave';
- $update_result = drupal_write_record('test_serialized', $person);
- $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical($result->name, 'Dave', 'Name field set.');
- $this->assertIdentical($result->info, NULL, 'Info field set.');
- $person->info = array();
- $update_result = drupal_write_record('test_serialized', $person, array('id'));
- $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical(unserialize($result->info), array(), 'Info field updated.');
-
- $data = array('foo' => 'bar', 1 => 2, 'empty' => '', 'null' => NULL);
- $person->info = $data;
- $update_result = drupal_write_record('test_serialized', $person, array('id'));
- $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject();
- $this->assertIdentical(unserialize($result->info), $data, 'Info field updated.');
-
-
-
- $update_result = drupal_write_record('test_null', $person, array('id'));
- $this->assertTrue($update_result == SAVED_UPDATED, 'Correct value returned when a valid update is run without changing any values.');
-
- $node_access = new stdClass();
- $node_access->nid = mt_rand();
- $node_access->gid = mt_rand();
- $node_access->realm = $this->randomName();
- $insert_result = drupal_write_record('node_access', $node_access);
- $this->assertTrue($insert_result == SAVED_NEW, 'Correct value returned when a record is inserted with drupal_write_record() for a table with a multi-field primary key.');
-
- $update_result = drupal_write_record('node_access', $node_access, array('nid', 'gid', 'realm'));
- $this->assertTrue($update_result == SAVED_UPDATED, 'Correct value returned when a record is updated with drupal_write_record() for a table with a multi-field primary key.');
- }
- }
- class DrupalErrorCollectionUnitTest extends DrupalWebTestCase {
-
- protected $collectedErrors = array();
- public static function getInfo() {
- return array(
- 'name' => 'SimpleTest error collector',
- 'description' => 'Performs tests on the Simpletest error and exception collector.',
- 'group' => 'SimpleTest',
- );
- }
- function setUp() {
- parent::setUp('system_test', 'error_test');
- }
-
- function testErrorCollect() {
- $this->collectedErrors = array();
- $this->drupalGet('error-test/generate-warnings-with-report');
- $this->assertEqual(count($this->collectedErrors), 3, 'Three errors were collected');
- if (count($this->collectedErrors) == 3) {
- $this->assertError($this->collectedErrors[0], 'Notice', 'error_test_generate_warnings()', 'error_test.module', 'Undefined variable: bananas');
- $this->assertError($this->collectedErrors[1], 'Warning', 'error_test_generate_warnings()', 'error_test.module', 'Division by zero');
- $this->assertError($this->collectedErrors[2], 'User warning', 'error_test_generate_warnings()', 'error_test.module', 'Drupal is awesome');
- }
- else {
-
- foreach ($this->collectedErrors as $error) {
- parent::error($error['message'], $error['group'], $error['caller']);
- }
- }
- }
-
- protected function error($message = '', $group = 'Other', array $caller = NULL) {
-
-
-
-
-
-
- if ($group == 'User notice') {
- parent::error($message, $group, $caller);
- }
-
- else {
- $this->collectedErrors[] = array(
- 'message' => $message,
- 'group' => $group,
- 'caller' => $caller
- );
- }
- }
-
- function assertError($error, $group, $function, $file, $message = NULL) {
- $this->assertEqual($error['group'], $group, format_string("Group was %group", array('%group' => $group)));
- $this->assertEqual($error['caller']['function'], $function, format_string("Function was %function", array('%function' => $function)));
- $this->assertEqual(drupal_basename($error['caller']['file']), $file, format_string("File was %file", array('%file' => $file)));
- if (isset($message)) {
- $this->assertEqual($error['message'], $message, format_string("Message was %message", array('%message' => $message)));
- }
- }
- }
- class ParseInfoFilesTestCase extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Parsing .info files',
- 'description' => 'Tests parsing .info files.',
- 'group' => 'System',
- );
- }
-
- function testParseInfoFile() {
- $info_values = drupal_parse_info_file(drupal_get_path('module', 'simpletest') . '/tests/common_test_info.txt');
- $this->assertEqual($info_values['simple_string'], 'A simple string', 'Simple string value was parsed correctly.', 'System');
- $this->assertEqual($info_values['simple_constant'], WATCHDOG_INFO, 'Constant value was parsed correctly.', 'System');
- $this->assertEqual($info_values['double_colon'], 'dummyClassName::', 'Value containing double-colon was parsed correctly.', 'System');
- }
- }
- class DrupalSystemListingTestCase extends DrupalWebTestCase {
-
- protected $profile = 'testing';
- public static function getInfo() {
- return array(
- 'name' => 'Drupal system listing',
- 'description' => 'Tests the mechanism for scanning system directories in drupal_system_listing().',
- 'group' => 'System',
- );
- }
-
- function testDirectoryPrecedence() {
-
-
- $expected_directories = array(
-
-
-
- 'drupal_system_listing_incompatible_test' => array(
- 'modules/simpletest/tests',
- 'profiles/testing/modules',
- ),
-
-
- 'drupal_system_listing_compatible_test' => array(
- 'profiles/testing/modules',
- 'modules/simpletest/tests',
- ),
- );
-
-
-
- foreach ($expected_directories as $module => $directories) {
- foreach ($directories as $directory) {
- $filename = "$directory/$module/$module.module";
- $this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $filename), format_string('@filename exists.', array('@filename' => $filename)));
- }
- }
-
-
- $files = drupal_system_listing('/\.module$/', 'modules', 'name', 1);
- foreach ($expected_directories as $module => $directories) {
- $expected_directory = array_shift($directories);
- $expected_filename = "$expected_directory/$module/$module.module";
- $this->assertEqual($files[$module]->uri, $expected_filename, format_string('Module @module was found at @filename.', array('@module' => $module, '@filename' => $expected_filename)));
- }
- }
- }
- class FormatDateUnitTest extends DrupalWebTestCase {
-
- const LANGCODE = 'xx';
- public static function getInfo() {
- return array(
- 'name' => 'Format date',
- 'description' => 'Test the format_date() function.',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('locale');
- variable_set('configurable_timezones', 1);
- variable_set('date_format_long', 'l, j. F Y - G:i');
- variable_set('date_format_medium', 'j. F Y - G:i');
- variable_set('date_format_short', 'Y M j - g:ia');
- variable_set('locale_custom_strings_' . self::LANGCODE, array(
- '' => array('Sunday' => 'domingo'),
- 'Long month name' => array('March' => 'marzo'),
- ));
- $this->refreshVariables();
- }
-
- function testAdminDefinedFormatDate() {
-
- $this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
- $this->drupalLogin($this->admin_user);
-
- $admin_date_format = 'j M y';
- $edit = array('date_format' => $admin_date_format);
- $this->drupalPost('admin/config/regional/date-time/formats/add', $edit, 'Add format');
-
- $admin_date_format_uppercase = 'j M Y';
- $edit = array('date_format' => $admin_date_format_uppercase);
- $this->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
- $this->assertText(t('Custom date format added.'));
-
- $edit = array(
- 'date_type' => 'Example Style',
- 'machine_name' => 'example_style',
- 'date_format' => $admin_date_format,
- );
- $this->drupalPost('admin/config/regional/date-time/types/add', $edit, 'Add date type');
-
- $edit = array(
- 'machine_name' => 'example_style_uppercase',
- 'date_type' => 'Example Style Uppercase',
- 'date_format' => $admin_date_format_uppercase,
- );
- $this->drupalPost('admin/config/regional/date-time/types/add', $edit, t('Add date type'));
- $this->assertText(t('New date type added successfully.'));
- $timestamp = strtotime('2007-03-10T00:00:00+00:00');
- $this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07', 'Test format_date() using an admin-defined date type.');
- $this->assertIdentical(format_date($timestamp, 'example_style_uppercase', '', 'America/Los_Angeles'), '9 Mar 2007', 'Test format_date() using an admin-defined date type with different case.');
- $this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'medium'), 'Test format_date() defaulting to medium when $type not found.');
- }
-
- function testFormatDate() {
- global $user, $language;
- $timestamp = strtotime('2007-03-26T00:00:00+00:00');
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test all parameters.');
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test translated format.');
- $this->assertIdentical(format_date($timestamp, 'custom', '\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'l, 25-Mar-07 17:00:00 PDT', 'Test an escaped format string.');
- $this->assertIdentical(format_date($timestamp, 'custom', '\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\domingo, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash character.');
- $this->assertIdentical(format_date($timestamp, 'custom', '\\\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\l, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash followed by escaped format string.');
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
-
- $admin_user = $this->drupalCreateUser(array('administer languages'));
- $this->drupalLogin($admin_user);
- $edit = array(
- 'langcode' => self::LANGCODE,
- 'name' => self::LANGCODE,
- 'native' => self::LANGCODE,
- 'direction' => LANGUAGE_LTR,
- 'prefix' => self::LANGCODE,
- );
- $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
-
- $test_user = $this->drupalCreateUser();
- $this->drupalLogin($test_user);
- $edit = array('language' => self::LANGCODE, 'mail' => $test_user->mail, 'timezone' => 'America/Los_Angeles');
- $this->drupalPost('user/' . $test_user->uid . '/edit', $edit, t('Save'));
-
- drupal_save_session(FALSE);
-
- $real_user = $user;
- $user = user_load($test_user->uid, TRUE);
- $real_language = $language->language;
- $language->language = $user->language;
-
- date_default_timezone_set(drupal_get_user_timezone());
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test a different language.');
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
- $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T'), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test custom date format.');
- $this->assertIdentical(format_date($timestamp, 'long'), 'domingo, 25. marzo 2007 - 17:00', 'Test long date format.');
- $this->assertIdentical(format_date($timestamp, 'medium'), '25. marzo 2007 - 17:00', 'Test medium date format.');
- $this->assertIdentical(format_date($timestamp, 'short'), '2007 Mar 25 - 5:00pm', 'Test short date format.');
- $this->assertIdentical(format_date($timestamp), '25. marzo 2007 - 17:00', 'Test default date format.');
-
- $user = $real_user;
- $language->language = $real_language;
-
- date_default_timezone_set(drupal_get_user_timezone());
- drupal_save_session(TRUE);
- }
- }
- class DrupalAttributesUnitTest extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'HTML Attributes',
- 'description' => 'Perform unit tests on the drupal_attributes() function.',
- 'group' => 'System',
- );
- }
-
- function testDrupalAttributes() {
-
- $this->assertIdentical(drupal_attributes(array('title' => '&"\'<>')), ' title="&"'<>"', 'HTML encode attribute values.');
-
- $attributes = array('class' => array('first', 'last'));
- $this->assertIdentical(drupal_attributes(array('class' => array('first', 'last'))), ' class="first last"', 'Concatenate multi-value attributes.');
-
- $this->assertIdentical(drupal_attributes(array('alt' => '')), ' alt=""', 'Empty attribute value #1.');
- $this->assertIdentical(drupal_attributes(array('alt' => NULL)), ' alt=""', 'Empty attribute value #2.');
-
- $attributes = array(
- 'id' => 'id-test',
- 'class' => array('first', 'last'),
- 'alt' => 'Alternate',
- );
- $this->assertIdentical(drupal_attributes($attributes), ' id="id-test" class="first last" alt="Alternate"', 'Multiple attributes.');
-
- $this->assertIdentical(drupal_attributes(array()), '', 'Empty attributes array.');
- }
- }
- class DrupalJSONTest extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'JSON',
- 'description' => 'Perform unit tests on the drupal_json_encode() and drupal_json_decode() functions.',
- 'group' => 'System',
- );
- }
-
- function testJSON() {
-
-
- $str = '';
- for ($i=0; $i < 128; $i++) {
- $str .= chr($i);
- }
-
-
- $html_unsafe = array('<', '>', '\'', '&');
-
- $html_unsafe_escaped = array('\u003C', '\u003E', '\u0027', '\u0026', '\u0022');
-
- $this->assertIdentical(strlen($str), 128, 'A string with the full ASCII table has the correct length.');
- foreach ($html_unsafe as $char) {
- $this->assertTrue(strpos($str, $char) > 0, format_string('A string with the full ASCII table includes @s.', array('@s' => $char)));
- }
-
- $json = drupal_json_encode($str);
- $this->assertTrue(strlen($json) > strlen($str), 'A JSON encoded string is larger than the source string.');
-
- $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".');
- $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".');
- $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".');
-
- $json_decoded = drupal_json_decode($json);
- $this->assertIdentical($str, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.');
-
-
- $source = array(TRUE, FALSE, 0, 1, '0', '1', $str, array('key1' => $str, 'key2' => array('nested' => TRUE)));
- $json = drupal_json_encode($source);
- foreach ($html_unsafe as $char) {
- $this->assertTrue(strpos($json, $char) === FALSE, format_string('A JSON encoded string does not contain @s.', array('@s' => $char)));
- }
-
- foreach ($html_unsafe_escaped as $char) {
- $this->assertTrue(strpos($json, $char) > 0, format_string('A JSON encoded string contains @s.', array('@s' => $char)));
- }
- $json_decoded = drupal_json_decode($json);
- $this->assertNotIdentical($source, $json, 'An array encoded in JSON is not identical to the source.');
- $this->assertIdentical($source, $json_decoded, 'Encoding structured data to JSON and decoding back results in the original data.');
- }
- }
- class DrupalGetRdfNamespacesTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'RDF namespaces XML serialization tests',
- 'description' => 'Confirm that the serialization of RDF namespaces via drupal_get_rdf_namespaces() is output and parsed correctly in the XHTML document.',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp('rdf', 'rdf_test');
- }
-
- function testGetRdfNamespaces() {
-
- $this->drupalGet('');
- $xml = new SimpleXMLElement($this->content);
- $ns = $xml->getDocNamespaces();
- $this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is displayed.');
- $this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', 'The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.');
- $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', 'Two prefixes can be assigned the same namespace.');
- $this->assertTrue(!isset($ns['dc']), 'A prefix with conflicting namespaces is discarded.');
- }
- }
- class DrupalAddFeedTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'drupal_add_feed() tests',
- 'description' => 'Make sure that drupal_add_feed() works correctly with various constructs.',
- 'group' => 'System',
- );
- }
-
- function testBasicFeedAddNoTitle() {
- $path = $this->randomName(12);
- $external_url = 'http://' . $this->randomName(12) . '/' . $this->randomName(12);
- $fully_qualified_local_url = url($this->randomName(12), array('absolute' => TRUE));
- $path_for_title = $this->randomName(12);
- $external_for_title = 'http://' . $this->randomName(12) . '/' . $this->randomName(12);
- $fully_qualified_for_title = url($this->randomName(12), array('absolute' => TRUE));
-
-
-
-
- $urls = array(
- 'path without title' => array(
- 'input_url' => $path,
- 'output_url' => url($path, array('absolute' => TRUE)),
- 'title' => '',
- ),
- 'external URL without title' => array(
- 'input_url' => $external_url,
- 'output_url' => $external_url,
- 'title' => '',
- ),
- 'local URL without title' => array(
- 'input_url' => $fully_qualified_local_url,
- 'output_url' => $fully_qualified_local_url,
- 'title' => '',
- ),
- 'path with title' => array(
- 'input_url' => $path_for_title,
- 'output_url' => url($path_for_title, array('absolute' => TRUE)),
- 'title' => $this->randomName(12),
- ),
- 'external URL with title' => array(
- 'input_url' => $external_for_title,
- 'output_url' => $external_for_title,
- 'title' => $this->randomName(12),
- ),
- 'local URL with title' => array(
- 'input_url' => $fully_qualified_for_title,
- 'output_url' => $fully_qualified_for_title,
- 'title' => $this->randomName(12),
- ),
- );
- foreach ($urls as $description => $feed_info) {
- drupal_add_feed($feed_info['input_url'], $feed_info['title']);
- }
- $this->drupalSetContent(drupal_get_html_head());
- foreach ($urls as $description => $feed_info) {
- $this->assertPattern($this->urlToRSSLinkPattern($feed_info['output_url'], $feed_info['title']), format_string('Found correct feed header for %description', array('%description' => $description)));
- }
- }
-
- function urlToRSSLinkPattern($url, $title = '') {
-
- $url = preg_replace('/([+?.*])/', '[$0]', $url);
- $generated_pattern = '%<link +rel="alternate" +type="application/rss.xml" +title="' . $title . '" +href="' . $url . '" */>%';
- return $generated_pattern;
- }
- }
- class FeedIconTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Feed icon',
- 'description' => 'Check escaping of theme_feed_icon()',
- 'group' => 'System',
- );
- }
-
- function testFeedIconEscaping() {
- $variables = array();
- $variables['url'] = 'node';
- $variables['title'] = '<>&"\'';
- $text = theme_feed_icon($variables);
- preg_match('/title="(.*?)"/', $text, $matches);
- $this->assertEqual($matches[1], 'Subscribe to &"'', 'theme_feed_icon() escapes reserved HTML characters.');
- }
- }
- class ArrayDiffUnitTest extends DrupalUnitTestCase {
-
- protected $array1;
-
- protected $array2;
- public static function getInfo() {
- return array(
- 'name' => 'Array differences',
- 'description' => 'Performs tests on drupal_array_diff_assoc_recursive().',
- 'group' => 'System',
- );
- }
- function setUp() {
- parent::setUp();
- $this->array1 = array(
- 'same' => 'yes',
- 'different' => 'no',
- 'array_empty_diff' => array(),
- 'null' => NULL,
- 'int_diff' => 1,
- 'array_diff' => array('same' => 'same', 'array' => array('same' => 'same')),
- 'array_compared_to_string' => array('value'),
- 'string_compared_to_array' => 'value',
- 'new' => 'new',
- );
- $this->array2 = array(
- 'same' => 'yes',
- 'different' => 'yes',
- 'array_empty_diff' => array(),
- 'null' => NULL,
- 'int_diff' => '1',
- 'array_diff' => array('same' => 'different', 'array' => array('same' => 'same')),
- 'array_compared_to_string' => 'value',
- 'string_compared_to_array' => array('value'),
- );
- }
-
- public function testArrayDiffAssocRecursive() {
- $expected = array(
- 'different' => 'no',
- 'int_diff' => 1,
-
- 'array_diff' => array('same' => 'same'),
- 'array_compared_to_string' => array('value'),
- 'string_compared_to_array' => 'value',
- 'new' => 'new',
- );
- $this->assertIdentical(drupal_array_diff_assoc_recursive($this->array1, $this->array2), $expected);
- }
- }
- class DrupalGetQueryArrayTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Query parsing using drupal_get_query_array()',
- 'description' => 'Tests that drupal_get_query_array() correctly parses query parameters.',
- 'group' => 'System',
- );
- }
-
- public function testDrupalGetQueryArray() {
- $url = "http://my.site.com/somepath?foo=/content/folder[@name='foo']/folder[@name='bar']";
- $parsed = parse_url($url);
- $result = drupal_get_query_array($parsed['query']);
- $this->assertEqual($result['foo'], "/content/folder[@name='foo']/folder[@name='bar']", 'drupal_get_query_array() should only explode parameters on the first equals sign.');
- }
- }
|