....................................../////.===Shadow-Here===./////................................................ > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < ------------------------------------------------------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// RIFF¤ WEBPVP8 ˜ ðÑ *ôô>‘HŸK¥¤"§£±¨àð enü¹%½_F‘åè¿2ºQú³íªú`N¿­3ÿƒügµJžaÿ¯ÿ°~¼ÎùnúîÞÖô•òíôÁÉß®Sm¥Ü/ ‡ó˜f£Ùà<˜„xëJ¢Ù€SO3x<ªÔ©4¿+ç¶A`q@Ì“Úñè™ÍÿJÌ´ª-˜ÆtÊÛL]Ïq*‘Ý”ì#ŸÌÏãY]@ê`¿ /ªfkØB4·®£ó z—Üw¥Pxù–ÞLШKÇN¾AkÙTf½è'‰g gÆv›Øuh~ a˜Z— ïj*á¥t d£“uÒ ¨`K˜¹ßþ]b>˜]_ÏÔ6W—è2r4x•íÖ…"ƒÖNîä!¦å Ú}ýxGøÌ —@ ;ÆÚŠ=ɾ1ý8lªË¥ô ^yf®Œ¢u&2©nÙÇ›ñÂñŒ³ aPo['½»øFùà­+4ê“$!lövlüÞ=;N®3ð‚õ›DÉKòÞ>ÄÍ ¥ˆuߤ#ˆ$6ù™¥îЇy’ÍB¼ çxÛ;X"WL£R÷͝*ó-¶Zu}º.s¸sšXqù–DþÿvªhüïwyŸ ¯é³lÀ:KCûÄ£Ëá\…­ ~—ýóî ¼ûûÜTÓüÇy…ŽÆvc»¾×U ñ¸žþоP÷¦ó:Ò¨¨5;Ð#&#ÖúñläÿÁœ GxÉ­/ñ‡áQðìYÉtÒw޼GÔ´zàÒò ð*ëzƒ•4~H]Ø‹f ñÓÈñ`NåWçs'ÆÏW^ø¹!XžµmQ5ÃËoLœÎ: ÞËÍ¥J ù…î èo£ßPÎñ¶ž8.Œ]ʵ~5›ÙË-ù*8ÙÖß±~ ©¹rÓê‚j¶d¸{^Q'˜±Crß ÚH—#¥¥QlÀ×ëã‡DÜ«èî þ&Çæžî;ŽÏºò6ÒLÃXy&ZŒ'j‚¢Ù€IßÚù+–MGi‰*jE€‘JcÜ ÓÌ EÏÚj]o˜ Þr <¾U ûŪæÍ/šÝH¥˜b”¼ ÁñßX GP›ï2›4WŠÏà×£…íÓk†¦H·ÅíMh–*nó÷à]ÁjCº€b7<ب‹¨5車bp2:Á[UªM„QŒçiNMa#<5›áËó¸HýÊ"…×Éw¹¦ì2º–x<›»a±¸3Weü®FÝ⑱ö–î–³|LPÈ~çð~Çå‡|º kD¢µÏàÆAI %1À% ¹Ò – ”ϝS¦‰4&¶£°à Öý”û_Ò Áw°A«Å€?mÇÛgHÉ/8)á¾ÛìáöŽP í¨PŸNÙµº¦‡§Ùš"ÿ«>+ªÕ`Ê÷‡‚ß Õû˜þãÇ-PÍ.¾XV‘€ dÜ"þ4¹ ±Oú‘©t¥¦FªÄÃÄ•b‚znýu½—#cDs˜ÃiÑOˆñ×QO=*IAÊ,¶ŽZƒ;‡wøXè%EÐk:F±Ú” .Ѽ+Áu&Ç`."pÈÉw o&¿dE6‘’EqTuK@Ì¥ã™À(Êk(h‰,H}RÀIXÛš3µ1©_OqÚÒJAñ$ÊÙÜ;D3çŒ[þùœh¬Ã³™ö6ç†NY".Ú‰ï[ªŸŒ '²Ð öø_¨ÂÉ9ué¶³ÒŠõTàîMØ#û¯gN‡bÙ놚X„ö …ÉeüÌ^J ‹€.œ$Æ)βÄeæW#óüßĺŸ€ ÀzwV 9oä»f4V*uB «Ë†¹ì¯žR霓æHXa=&“I4K;¯ç‹h×·"UŠ~<•╪Vêª&ÍSÃÆÅ?ÔqÎ*mTM ˜›µwêd#[C¡©§‘D<©àb†–ÁœøvH/,í:¯( ²£|4-„Æövv„Yͼ™^Á$ˆ„¢Û[6yB.åH*V¨æ?$=˜Ñ€•ñ·­(VlŸ‘ nÀt8W÷´Bûba?q9ú¶Xƒl«ÿ\ù¶’þòUÐj/õ¢Ìµ³g$ƒÎR!¸»|Oߍë’BhîÚÑ¢ñåŒJ„®„£2Ð3•ô02Nt…!£Í]Ïc½Qÿ?ˆ<&ÃA¾Ú,JˆijÌ#5yz„‰Î|ÊŽ5QÏ:‹ÐaóVÔxW—CpeÏzÐïíçôÿÅ_[hãsÐ_/ŽTÝ?BîˆííV$<¿i>²F¬_Eß¿ †bÊŒº­ÿ®Z H“C}”¬,Mp ý/Bá£w>˜YV°aƒúh+cŠ- r/[%|üUMHäQ°X»|û/@|°¥Ð !BÔ Ç¢Ä©š+Õì D«7ìN¶ŽðÔ " ƶ’ÖçtA‰Û×}{tþz­¾GÍ›k¹OEJR$ Â׃ «ëÁ"oÉôž$oUK(Ä)Ãz³Ê-‹êN[Ò3Œñbï8P 4ƒ×q¢bo|?<ÛX¬òÄͰL–±›(™ûG?ýË©ÚÄ–ÂDØÐ_Ç¡ô ¾–ÄÏø ×e8Ë©$ÄF¹Å‹ì[©óìl:F¾f´‹‹Xì²ï®\¬ôùƒ ÿat¥óèÒùHß0äe‚;ü×h:ÆWðHž=Ã8骣"kœ'Y?³}Tûè€>?0l›e1Lòñ„aæKÆw…hÖŠùW…ÈÆÄ0ši·›[pcwËþñiêíY/~-Á5˜!¿†A›™Mÿþ(±“t@â“ö2­´TG5yé]çå僳 .·ÍïçÝ7UÚ±Ð/Nè»,_Ï ùdj7\ï Wì4›„»c¸àešg#ÒÊ⥭áØo5‘?ÌdÝô¯ ¹kzsƒ=´#ëÉK›Ø´±-¥eW?‡çßtòTã…$Ý+qÿ±ƒ÷_3Ô¥í÷:æ–ž<·Ö‡‰Å¢ š‡%Ô—utÌÈìðžgÖÀz²À—ï÷Óîäõ{K'´È÷³yaÏÁjƒô}ž§®æÊydÕÈë5¯èˆõvÕ©ã*çD„ “z„Ó‡^^xÂ3M§A´JG‚öï 3W'ˆ.OvXè¡ÊÕª?5º7†˜(˜Ç¶#çê’¶!ÌdZK§æ 0fãaN]òY³RV ™î$®K2R¨`W!1Ôó\;Ý ýB%qæK•&ÓÈe9È0êI±žeŸß -ú@žQr¦ ö4»M¼Áè¹µmw 9 EÆE_°2ó„ŸXKWÁ×Hóì^´²GѝF©óäR†¦‰ç"V»eØ<3ùd3ÿÚ¤Žú“Gi" —‘_ÙËÎ~Üö¯¥½Î»üŸEÚŽåmÞþí ;ÞólËΦMzA"Âf(´òá;Éï(/7½ûñÌ­cïÕçлþÝz¾-ÍvÑ“pH­–ðÓj$¸Äû¤‚‘ãUBË-n“2åPkS5&‹Â|+g^œ®Ì͆d!OïäîU«c;{Û!ÅŽ«ëZ9Ókóˆ]¯ƒ›né `ÇÒ+tÆš (ØKá¾—=3œ®•vuMñg²\ï Ec€ 05±d™‡×iÇ×›UúvÌ¢£Èþ¡ÕØô¶ßÎA"ß±#Ö²ˆÊŸ¦*Ä~ij|àø.-¼'»Ú¥£h ofº¦‡VsR=N½„Î v˜Z*SÌ{=jÑB‹tê…;’HžH¯8–îDù8ñ¢|Q•bÛçš–‹m³“ê¨ åÏ^m¬Žãþ©ïêO‡½6] µÆ„Ooòü ²x}N¦Ë3ïé¿»€›HA˜m%çÞ/¿í7Fø“‹léUk)É°Œµ8Q8›:ÀŠeT*šõ~ôڝG6 ¢}`ùH­–”¡k ‰P1>š†®9z11!X wKfmÁ¦xÑ,N1Q”–æB¶M…ÒÃv6SMˆhU¬ÊPŽï‘öj=·CŒ¯u¹ƒVIЃsx4’ömÛýcå¡¶7ßŠß 57^\wÒÐÆ k§h,Œý î«q^R½3]J¸ÇðN ‚çU¬ôº^Áì} ³f©Õœ§ˆã:FÄÈ‚é(€™?àýÓüè1Gô£¼éj‚OÅñ  #>×—ßtà 0G¥Åa뀐kßhc™À_ÉñÞ#±)GD" YîäË-ÿÙ̪ ¹™a¯´¢E\ÝÒö‚;™„ë]_ p8‰o¡ñ+^÷ 3‘'dT4œŽ ðVë½° :¬víÑ«£tßÚS-3¶“þ2 †üüʨòrš¹M{É_¤`Û¨0ìjœøJ‡:÷ÃáZ˜†@GP&œÑDGÏs¡þ¦þDGú‘1Yá9Ôþ¼ ûø…§÷8&–ÜÑnÄ_m®^üÆ`;ÉVÁJ£?â€-ßê}suÍ2sõA NÌúA磸‘îÿÚ»ƒìö·á¿±tÑÐ"Tÿü˜[@/äj¬€uüªìù¥Ý˜á8Ý´sõj 8@rˆð äþZÇD®ÿUÏ2ùôõrBzÆÏÞž>Ì™xœ“ wiÎ×7_… ¸ \#€MɁV¶¥üÕÿPÔ9Z‡ø§É8#H:ƒ5ÀÝå9ÍIŒ5åKÙŠ÷qÄ>1AÈøžj"µÂд/ªnÀ qªã}"iŸBå˜ÓÛŽ¦…&ݧ;G@—³b¯“•"´4í¨ôM¨åñC‹ïùÉó¯ÓsSH2Ý@ßáM‡ˆKÀªÛUeø/4\gnm¥‹ŸŒ qÄ b9ÞwÒNÏ_4Ég³ú=܆‚´ •â¥õeíþkjz>éÚyU«Íӝ݃6"8/ø{=Ô¢»G¥ äUw°W«,ô—¿ãㆅү¢³xŠUû™yŒ (øSópÐ 9\åTâ»—*oG$/×ÍT†Y¿1¤Þ¢_‡ ¼ „±ÍçèSaÓ 3ÛMÁBkxs‰’R/¡¤ˆÙçª(*õ„üXÌ´ƒ E§´¬EF"Ù”R/ÐNyÆÂ^°?™6¡œïJ·±$§?º>ÖüœcNÌù¯G ‹ñ2ЁBB„^·úìaz¨k:#¨Æ¨8LÎõލ£^§S&cŒÐU€ü(‡F±Š¼&P>8ÙÁ ‰ p5?0ÊÆƒZl¸aô š¼¡}gÿ¶zÆC²¹¬ÎÖG*HB¡O<º2#ñŒAƒ–¡B˜´É$¥›É:FÀÔx¾u?XÜÏÓvN©RS{2ʈãk9rmP¼Qq̳ è¼ÐFׄ^¡Öì fE“F4A…!ì/…¦Lƒ… … $%´¾yã@CI¬ á—3PþBÏNÿ<ý°4Ü ËÃ#ØÍ~âW«rEñw‹eùMMHß²`¬Öó½íf³:‹k˜¯÷}Z!ã¿<¥,\#öµÀ¯aÒNÆIé,Ћ–lŽ#Àæ9ÀÒS·I’½-Ïp Äz¤Š Â* ­íÄ9­< h>׍3ZkËU¹§˜ŒŠ±f­’¤º³Q ÏB?‹#µíÃ¥®@(Gs«†vI¥Mµ‹Á©e~2ú³ÁP4ìÕi‚²Ê^ö@-DþÓàlÜOÍ]n"µã:žpsŽ¢:! Aõ.ç~ÓBûH÷JCÌ]õVƒd «ú´QÙEA–¯¯Œ!.ˆˆëQ±ù œ·Ì!Õâ )ùL„ÅÀlÚè5@B…o´Æ¸XÓ&Û…O«˜”_#‡ƒ„ûÈt!¤ÁÏ›ÎÝŠ?c9 â\>lÓÁVÄÑ™£eØY]:fÝ–—ù+p{™ðè û³”g±OƒÚSù£áÁÊ„ä,ï7š²G ÕÌBk)~ÑiCµ|h#u¤¶îK¨² #²vݯGãeÖ϶ú…¾múÀ¶þÔñ‚Š9'^($¤§ò “š½{éúp÷J›ušS¹áªCÂubÃH9™D™/ZöØÁ‡¦ÝÙŸ·kð*_”.C‹{áXó€‡c¡c€§/šò/&éš÷,àéJþ‰X›fµ“C¨œ®r¬"kL‰Â_q…Z–.ÉL~O µ›zn‚¹À¦Öª7\àHµšÖ %»ÇníV[¥*Õ;ƒ#½¾HK-ÖIÊdÏEÚ#=o÷Óò³´Š: Ç?{¾+9›–‘OEáU·S€˜j"ÄaÜ ŒÛWt› á–c#a»pÔZÞdŽtWê=9éöÊ¢µ~ ë ;Öe‡Œ®:bî3±ýê¢wà¼îpêñ¹¾4 zc¾ðÖÿzdêŒÑÒŝÀ‰s6¤í³ÎÙB¿OZ”+F¤á‡3@Ñëäg©·Ž ˆèª<ù@É{&S„œÕúÀA)‰h:YÀ5^ÂÓŒ°õäU\ ùËÍû#²?Xe¬tu‰^zÒÔãë¼ÛWtEtû …‚g¶Úüâî*moGè¨7%u!]PhÏd™Ý%Îx: VÒ¦ôÊD3ÀŽKÛËãvÆî…N¯ä>Eró–ð`5 Œ%u5XkñÌ*NU%¶áœÊ:Qÿú»“úzyÏ6å-၇¾ ´ ÒÊ]y žO‘w2Äøæ…H’²f±ÎÇ.ª|¥'gîV•Ü .̘¯€šòü¤U~Ù†*¢!?ò wý,}´°ÔÞnïoKq5µb!áÓ3"vAßH¡³¡·G(ÐÎ0Îò¼MG!/ài®@—¬04*`…«é8ªøøló“ˆÊ”èù¤…ßÊoÿé'ËuÌÖ5×È¡§ˆˆfŽë9}hìâ_!!¯  B&Ëö¶‰ÀAÙNVŸ Wh›¸®XÑJì¨ú“¿÷3uj²˜¨ÍÎìë±aúŠÝå¯ð*Ó¨ôJ“yºØ)m°WýOè68†ŸÏ2—‰Ïüꪫٚ¥‹l1 ø ÏÄFjêµvÌbü¦èÝx:X±¢H=MÐß—,ˆÉÇ´(9ú¾^ÅÚ4¿m‡$âX‘å%(AlZo@½¨UOÌÕ”1ø¸jÎÀÃÃ_ µ‘Ü.œº¦Ut: Æï’!=¯uwû#,“pþÇúŒø(é@?³ü¥‘Mo §—s@Œ#)§ŒùkL}NOÆêA›¸~r½¼ÙA—HJ«eˆÖ´*¡ÓpÌŸö.m<-"³ûÈ$¬_6­åf£ïÚâj1y§ÕJ½@dÞÁr&Í\Z%D£Íñ·AZ Û³øüd/ªAi†/Й~  ‡âĮҮÏh§°b—›Û«mJžòG'[ÈYýŒ¦9psl ýÁ ®±f¦x,‰½tN ‚Xª9 ÙÖH.«Lo0×?͹m¡å†Ѽ+›2ƒF ±Ê8 7Hցϓ²Æ–m9…òŸï]Â1äN†VLâCˆU .ÿ‰Ts +ÅÎx(%¦u]6AF Š ØF鈄‘ |¢¶c±soŒ/t[a¾–û:s·`i햍ê›ËchÈ…8ßÀUÜewŒðNOƒõD%q#éû\9¤x¹&UE×G¥ Í—™$ð E6-‡¼!ýpãÔM˜ Âsìe¯ñµK¢Ç¡ùôléœ4Ö£”À Š®Ðc ^¨À}ÙËŸ§›ºê{ÊuÉC ×Sr€¤’fÉ*j!úÓ’Gsùìoîßîn%ò· àc Wp÷$¨˜)û»H ×8ŽÒ€Zj¤3ÀÙºY'Ql¦py{-6íÔCeiØp‘‡XÊîÆUߢ܂ž£Xé¼Y8þ©ëgñß}é.ÎógÒ„ÃØËø¯»™§Xýy M%@NŠ À(~áÐvu7&•,Ù˜ó€uP‡^^®=_E„jt’ 403WebShell
403Webshell
Server IP : 41.207.40.59  /  Your IP : 216.73.216.58
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux srv-prod 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User : www-data ( 33)
PHP Version : 8.3.26
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/html/formation/lib/tests/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/formation/lib/tests/navigationlib_test.php
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace core;

use action_link;
use global_navigation;
use navbar;
use navigation_cache;
use navigation_node;
use navigation_node_collection;
use pix_icon;
use popup_action;
use settings_navigation;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/navigationlib.php');

/**
 * Unit tests for lib/navigationlib.php
 *
 * @package   core
 * @category  test
 * @copyright 2009 Sam Hemelryk
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
 */
final class navigationlib_test extends \advanced_testcase {
    /**
     * @var navigation_node
     */
    public $node;

    protected function setup_node() {
        global $PAGE, $SITE;

        $PAGE->set_url('/');
        $PAGE->set_course($SITE);

        $activeurl = $PAGE->url;
        $inactiveurl = new \moodle_url('http://www.moodle.com/');

        navigation_node::override_active_url($PAGE->url);

        $this->node = new navigation_node('Test Node');
        $this->node->type = navigation_node::TYPE_SYSTEM;
        // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
        $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
        $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
        $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
        $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
        $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE,  null, 'demo4', new pix_icon('i/course', ''));
        $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
        $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
        $demo6 = $demo3->add('demo6', null, navigation_node::TYPE_CONTAINER, 'container node test', 'demo6');
        $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
        $hiddendemo1->hidden = true;
        $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
        $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
    }

    public function test_node__construct(): void {
        $this->setup_node();

        $fakeproperties = array(
            'text' => 'text',
            'shorttext' => 'A very silly extra long short text string, more than 25 characters',
            'key' => 'key',
            'type' => 'navigation_node::TYPE_COURSE',
            'action' => new \moodle_url('http://www.moodle.org/'));

        $node = new navigation_node($fakeproperties);
        $this->assertSame($fakeproperties['text'], $node->text);
        $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
        $this->assertSame($fakeproperties['key'], $node->key);
        $this->assertSame($fakeproperties['type'], $node->type);
        $this->assertSame($fakeproperties['action'], $node->action);
    }

    public function test_node_add(): void {
        $this->setup_node();

        // Add a node with all args set.
        $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
        // Add a node with the minimum args required.
        $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
        $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));

        $this->assertInstanceOf('navigation_node', $node1);
        $this->assertInstanceOf('navigation_node', $node2);
        $this->assertInstanceOf('navigation_node', $node3);

        $ref = $this->node->get('key');
        $this->assertSame($node1, $ref);

        $ref = $this->node->get($node2->key);
        $this->assertSame($node2, $ref);

        $ref = $this->node->get($node2->key, $node2->type);
        $this->assertSame($node2, $ref);

        $ref = $this->node->get($node3->key, $node3->type);
        $this->assertSame($node3, $ref);
    }

    public function test_node_add_before(): void {
        $this->setup_node();

        // Create 3 nodes.
        $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
            'test 1', 'testadd1');
        $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
            'test 2', 'testadd2');
        $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
            'test 3', 'testadd3');
        // Add node 2, then node 1 before 2, then node 3 at end.
        $this->node->add_node($node2);
        $this->node->add_node($node1, 'testadd2');
        $this->node->add_node($node3);
        // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
        foreach ($this->node->children as $child) {
            $keys[] = $child->key;
        }
        $this->assertSame('testadd1', $keys[count($keys)-3]);
        $this->assertSame('testadd2', $keys[count($keys)-2]);
        $this->assertSame('testadd3', $keys[count($keys)-1]);
    }

    public function test_node_add_class(): void {
        $this->setup_node();

        $node = $this->node->get('demo1');
        $this->assertInstanceOf('navigation_node', $node);
        if ($node !== false) {
            $node->add_class('myclass');
            $classes = $node->classes;
            $this->assertContains('myclass', $classes);
        }
    }

    /**
     * Test the add_attribute method.
     * @covers \navigation_node::add_attribute
     */
    public function test_node_add_attribute(): void {
        $this->setup_node();

        $node = $this->node->get('demo1');
        $this->assertInstanceOf('navigation_node', $node);
        if ($node !== false) {
            $node->add_attribute('data-foo', 'bar');
            $attribute = reset($node->attributes);
            $this->assertEqualsCanonicalizing(['name' => 'data-foo', 'value' => 'bar'], $attribute);
        }
    }

    public function test_node_check_if_active(): void {
        $this->setup_node();

        // First test the string urls
        // Demo1 -> action is http://www.moodle.org/, thus should be true.
        $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
        if ($this->assertInstanceOf('navigation_node', $demo5)) {
            $this->assertTrue($demo5->check_if_active());
        }

        // Demo2 -> action is http://www.moodle.com/, thus should be false.
        $demo2 = $this->node->get('demo2');
        if ($this->assertInstanceOf('navigation_node', $demo2)) {
            $this->assertFalse($demo2->check_if_active());
        }
    }

    public function test_node_contains_active_node(): void {
        $this->setup_node();

        // Demo5, and activity1 were set to active during setup.
        // Should be true as it contains all nodes.
        $this->assertTrue($this->node->contains_active_node());
        // Should be true as demo5 is a child of demo3.
        $this->assertTrue($this->node->get('demo3')->contains_active_node());
        // Obviously duff.
        $this->assertFalse($this->node->get('demo1')->contains_active_node());
        // Should be true as demo5 contains activity1.
        $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
        // Should be true activity1 is the active node.
        $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
        // Obviously duff.
        $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
    }

    public function test_node_find_active_node(): void {
        $this->setup_node();

        $activenode1 = $this->node->find_active_node();
        $activenode2 = $this->node->get('demo1')->find_active_node();

        if ($this->assertInstanceOf('navigation_node', $activenode1)) {
            $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
            $this->assertSame($activenode1, $ref);
        }

        $this->assertNotInstanceOf('navigation_node', $activenode2);
    }

    public function test_node_find(): void {
        $this->setup_node();

        $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
        $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
        $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
        $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
        $this->assertInstanceOf('navigation_node', $node1);
        $this->assertInstanceOf('navigation_node', $node2);
        $this->assertNotInstanceOf('navigation_node', $node3);
        $this->assertNotInstanceOf('navigation_node', $node4);
    }

    public function test_node_find_expandable(): void {
        $this->setup_node();

        $expandable = array();
        $this->node->find_expandable($expandable);

        $this->assertCount(0, $expandable);
        if (count($expandable) === 4) {
            $name = $expandable[0]['key'];
            $name .= $expandable[1]['key'];
            $name .= $expandable[2]['key'];
            $name .= $expandable[3]['key'];
            $this->assertSame('demo1demo2demo4hiddendemo2', $name);
        }
    }

    public function test_node_get(): void {
        $this->setup_node();

        $node1 = $this->node->get('demo1'); // Exists.
        $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
        $node3 = $this->node->get('demo0'); // Doesn't exist at all.
        $node4 = $this->node->get(false);   // Sometimes occurs in nature code.
        $this->assertInstanceOf('navigation_node', $node1);
        $this->assertFalse($node2);
        $this->assertFalse($node3);
        $this->assertFalse($node4);
    }

    public function test_node_get_css_type(): void {
        $this->setup_node();

        $csstype1 = $this->node->get('demo3')->get_css_type();
        $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
        $this->node->get('demo3')->get('demo5')->type = 1000;
        $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
        $csstype4 = $this->node->get('demo3')->get('demo6')->get_css_type();
        $this->assertSame('type_category', $csstype1);
        $this->assertSame('type_course', $csstype2);
        $this->assertSame('type_unknown', $csstype3);
        $this->assertSame('type_container', $csstype4);
    }

    public function test_node_make_active(): void {
        global $CFG;
        $this->setup_node();

        $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
        $node2 = $this->node->add('active node 2', new \moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
        $node1->make_active();
        $this->node->get('anode2')->make_active();
        $this->assertTrue($node1->isactive);
        $this->assertTrue($this->node->get('anode2')->isactive);
    }

    public function test_node_remove(): void {
        $this->setup_node();

        $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
        $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
        $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');

        $this->assertInstanceOf('navigation_node', $remove1);
        $this->assertInstanceOf('navigation_node', $remove2);
        $this->assertInstanceOf('navigation_node', $remove3);

        $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
        $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
        $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));

        // Remove element and make sure this is no longer a child.
        $this->assertTrue($remove1->remove());
        $this->assertFalse($this->node->get('remove1'));
        $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));

        // Make sure that we can insert element after removal.
        $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
        $this->node->add_node($insertelement, 'remove2');
        $this->assertNotEmpty($this->node->get('element4'));

        // Remove more elements.
        $this->assertTrue($this->node->get('remove2')->remove());
        $this->assertFalse($this->node->get('remove2'));

        // Make sure that we can add element after removal.
        $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
        $this->assertNotEmpty($this->node->get('element5'));

        $this->assertTrue($remove2->get('remove3')->remove());

        $this->assertFalse($this->node->get('remove1'));
        $this->assertFalse($this->node->get('remove2'));
    }

    public function test_node_remove_class(): void {
        $this->setup_node();

        $this->node->add_class('testclass');
        $this->assertTrue($this->node->remove_class('testclass'));
        $this->assertNotContains('testclass', $this->node->classes);
    }

    public function test_module_extends_navigation(): void {
        $node = new exposed_global_navigation();
        // Create an initial tree structure to work with.
        $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
        $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
        $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
        $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
        $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
        $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
        $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
        $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
        $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
        $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
        $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
        $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
        $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
        $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
        $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
        $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
        $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
        $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');

        $this->assertTrue($node->exposed_module_extends_navigation('data'));
        $this->assertFalse($node->exposed_module_extends_navigation('test1'));
    }

    public function test_navbar_prepend_and_add(): \moodle_page {
        global $PAGE;
        // Unfortunate hack needed because people use global $PAGE around the place.
        $PAGE->set_url('/');

        // We need to reset after this test because we using the generator.
        $this->resetAfterTest();

        $generator = self::getDataGenerator();
        $cat1 = $generator->create_category();
        $cat2 = $generator->create_category(array('parent' => $cat1->id));
        $course = $generator->create_course(array('category' => $cat2->id));

        $page = new \moodle_page();
        $page->set_course($course);
        $page->set_url(new \moodle_url('/course/view.php', array('id' => $course->id)));
        $page->navbar->prepend('test 1');
        $page->navbar->prepend('test 2');
        $page->navbar->add('test 3');
        $page->navbar->add('test 4');

        $items = $page->navbar->get_items();
        foreach ($items as $item) {
            $this->assertInstanceOf('navigation_node', $item);
        }

        $i = 0;
        $this->assertSame('test 1', $items[$i++]->text);
        $this->assertSame('test 2', $items[$i++]->text);
        $this->assertSame('home', $items[$i++]->key);
        $this->assertSame('courses', $items[$i++]->key);
        $this->assertSame($cat1->id, $items[$i++]->key);
        $this->assertSame($cat2->id, $items[$i++]->key);
        $this->assertSame($course->id, $items[$i++]->key);
        $this->assertSame('test 3', $items[$i++]->text);
        $this->assertSame('test 4', $items[$i++]->text);

        return $page;
    }

    /**
     * @depends test_navbar_prepend_and_add
     * @param $node
     */
    public function test_navbar_has_items(\moodle_page $page): void {
        $this->resetAfterTest();

        $this->assertTrue($page->navbar->has_items());
    }

    public function test_cache__get(): void {
        $cache = new navigation_cache('unittest_nav');
        $cache->anysetvariable = true;

        $this->assertTrue($cache->anysetvariable);
        $this->assertEquals($cache->notasetvariable, null);
    }

    public function test_cache__set(): void {
        $cache = new navigation_cache('unittest_nav');
        $cache->anysetvariable = true;

        $cache->myname = 'Sam Hemelryk';
        $this->assertTrue($cache->cached('myname'));
        $this->assertSame('Sam Hemelryk', $cache->myname);
    }

    public function test_cache_cached(): void {
        $cache = new navigation_cache('unittest_nav');
        $cache->anysetvariable = true;

        $this->assertTrue($cache->cached('anysetvariable'));
        $this->assertFalse($cache->cached('notasetvariable'));
    }

    public function test_cache_clear(): void {
        $cache = new navigation_cache('unittest_nav');
        $cache->anysetvariable = true;

        $cache = clone($cache);
        $this->assertTrue($cache->cached('anysetvariable'));
        $cache->clear();
        $this->assertFalse($cache->cached('anysetvariable'));
    }

    public function test_cache_set(): void {
        $cache = new navigation_cache('unittest_nav');
        $cache->anysetvariable = true;

        $cache->set('software', 'Moodle');
        $this->assertTrue($cache->cached('software'));
        $this->assertEquals($cache->software, 'Moodle');
    }

    public function test_setting___construct(): settings_navigation {
        global $PAGE, $SITE;

        $this->resetAfterTest(false);

        $PAGE->set_url('/');
        $PAGE->set_course($SITE);

        $node = new exposed_settings_navigation();

        return $node;
    }

    /**
     * @depends test_setting___construct
     * @param mixed $node
     * @return mixed
     */
    public function test_setting__initialise($node): settings_navigation {
        $this->resetAfterTest(false);

        $node->initialise();
        $this->assertEquals($node->id, 'settingsnav');

        return $node;
    }

    /**
     * Test that users with the correct permissions can view the preferences page.
     */
    public function test_can_view_user_preferences(): void {
        global $PAGE, $DB, $SITE;
        $this->resetAfterTest();

        $persontoview = $this->getDataGenerator()->create_user();
        $persondoingtheviewing = $this->getDataGenerator()->create_user();

        $PAGE->set_url('/');
        $PAGE->set_course($SITE);

        // Check that a standard user can not view the preferences page.
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
        $this->getDataGenerator()->role_assign($studentrole->id, $persondoingtheviewing->id);
        $this->setUser($persondoingtheviewing);
        $settingsnav = new exposed_settings_navigation();
        $settingsnav->initialise();
        $settingsnav->extend_for_user($persontoview->id);
        $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id));

        // Set persondoingtheviewing as a manager.
        $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
        $this->getDataGenerator()->role_assign($managerrole->id, $persondoingtheviewing->id);
        $settingsnav = new exposed_settings_navigation();
        $settingsnav->initialise();
        $settingsnav->extend_for_user($persontoview->id);
        $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));

        // Check that the admin can view the preferences page.
        $this->setAdminUser();
        $settingsnav = new exposed_settings_navigation();
        $settingsnav->initialise();
        $settingsnav->extend_for_user($persontoview->id);
        $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id, null);
        $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
    }

    /**
     * @depends test_setting__initialise
     * @param mixed $node
     * @return mixed
     */
    public function test_setting_in_alternative_role($node): void {
        $this->resetAfterTest();

        $this->assertFalse($node->exposed_in_alternative_role());
    }


    public function test_navigation_node_collection_remove_with_no_type(): void {
        $navigationnodecollection = new navigation_node_collection();
        $this->setup_node();
        $this->node->key = 100;

        // Test it's empty
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));

        // Add a node
        $navigationnodecollection->add($this->node);

        // Test it's not empty
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));

        // Remove a node - passing key only!
        $this->assertTrue($navigationnodecollection->remove(100));

        // Test it's empty again!
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
    }

    public function test_navigation_node_collection_remove_with_type(): void {
        $navigationnodecollection = new navigation_node_collection();
        $this->setup_node();
        $this->node->key = 100;

        // Test it's empty
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));

        // Add a node
        $navigationnodecollection->add($this->node);

        // Test it's not empty
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));

        // Remove a node - passing type
        $this->assertTrue($navigationnodecollection->remove(100, 1));

        // Test it's empty again!
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
    }

    /**
     * Test the set_force_into_more_menu method.
     *
     * @param bool $haschildren       Whether the navigation node has children nodes
     * @param bool $forceintomoremenu Whether to force the navigation node and its children into the "more" menu
     * @dataProvider set_force_into_more_menu_provider
     */
    public function test_set_force_into_more_menu(bool $haschildren, bool $forceintomoremenu): void {
        // Create a navigation node.
        $node = new navigation_node(['text' => 'Navigation node', 'key' => 'navnode']);

        // If required, add some children nodes to the navigation node.
        if ($haschildren) {
            for ($i = 1; $i <= 3; $i++) {
                $node->add("Child navigation node {$i}");
            }
        }

        $node->set_force_into_more_menu($forceintomoremenu);
        // Assert that the expected value has been assigned to the 'forceintomoremenu' property
        // in the navigation node and its children.
        $this->assertEquals($forceintomoremenu, $node->forceintomoremenu);
        foreach ($node->children as $child) {
            $this->assertEquals($forceintomoremenu, $child->forceintomoremenu);
        }
    }

    /**
     * Data provider for the test_set_force_into_more_menu function.
     *
     * @return array
     */
    public static function set_force_into_more_menu_provider(): array {
        return [
            'Navigation node without any children nodes; Force into "more" menu => true.' =>
                [
                    false,
                    true,
                ],
            'Navigation node with children nodes; Force into "more" menu => true.' =>
                [
                    true,
                    true,
                ],
            'Navigation node with children nodes; Force into "more" menu => false.' =>
                [
                    true,
                    false,
                ],
        ];
    }

    /**
     * Test the is_action_link method.
     *
     * @param navigation_node $node The sample navigation node
     * @param bool $expected Whether the navigation node contains an action link
     * @dataProvider is_action_link_provider
     * @covers navigation_node::is_action_link
     */
    public function test_is_action_link(navigation_node $node, bool $expected): void {
        $this->assertEquals($node->is_action_link(), $expected);
    }

    /**
     * Data provider for the test_is_action_link function.
     *
     * @return array
     */
    public static function is_action_link_provider(): array {
        return [
            'The navigation node has an action link.' =>
                [
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
                    true
                ],

            'The navigation node does not have an action link.' =>
                [
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
                    false
                ],
        ];
    }

    /**
     * Test the action_link_actions method.
     *
     * @param navigation_node $node The sample navigation node
     * @dataProvider action_link_actions_provider
     * @covers navigation_node::action_link_actions
     */
    public function test_action_link_actions(navigation_node $node): void {
        // Get the formatted array of action link actions.
        $data = $node->action_link_actions();
        // The navigation node has an action link.
        if ($node->action instanceof action_link) {
            if (!empty($node->action->actions)) { // There are actions added to the action link.
                $this->assertArrayHasKey('actions', $data);
                $this->assertCount(1, $data['actions']);
                $expected = (object)[
                    'id' => $node->action->attributes['id'],
                    'event' => $node->action->actions[0]->event,
                    'jsfunction' => $node->action->actions[0]->jsfunction,
                    'jsfunctionargs' => json_encode($node->action->actions[0]->jsfunctionargs)
                ];
                $this->assertEquals($expected, $data['actions'][0]);
            } else { // There are no actions added to the action link.
                $this->assertArrayHasKey('actions', $data);
                $this->assertEmpty($data['actions']);
            }
        } else { // The navigation node does not have an action link.
            $this->assertEmpty($data);
        }
    }

    /**
     * Data provider for the test_action_link_actions function.
     *
     * @return array
     */
    public static function action_link_actions_provider(): array {
        return [
            'The navigation node has an action link with an action attached.' =>
                [
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
                ],
            'The navigation node has an action link without an action.' =>
                [
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '', null),
                        navigation_node::TYPE_SETTING),
                ],
            'The navigation node does not have an action link.' =>
                [
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
                ],
        ];
    }
}


/**
 * This is a dummy object that allows us to call protected methods within the
 * global navigation class by prefixing the methods with `exposed_`
 */
class exposed_global_navigation extends global_navigation {
    protected $exposedkey = 'exposed_';
    public function __construct(?\moodle_page $page=null) {
        global $PAGE;
        if ($page === null) {
            $page = $PAGE;
        }
        parent::__construct($page);
    }
    public function __call($method, $arguments) {
        if (strpos($method, $this->exposedkey) !== false) {
            $method = substr($method, strlen($this->exposedkey));
        }
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $arguments);
        }
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
    }
    public function set_initialised() {
        $this->initialised = true;
    }
}


class mock_initialise_global_navigation extends global_navigation {

    protected static $count = 1;

    public function load_for_category() {
        $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
        self::$count++;
        return 0;
    }

    public function load_for_course() {
        $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
        self::$count++;
        return 0;
    }

    public function load_for_activity() {
        $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
        self::$count++;
        return 0;
    }

    public function load_for_user($user=null, $forceforcontext=false) {
        $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
        self::$count++;
        return 0;
    }
}

/**
 * This is a dummy object that allows us to call protected methods within the
 * global navigation class by prefixing the methods with `exposed_`.
 */
class exposed_navbar extends navbar {
    protected $exposedkey = 'exposed_';

    public function __construct(\moodle_page $page) {
        parent::__construct($page);
    }
    public function __call($method, $arguments) {
        if (strpos($method, $this->exposedkey) !== false) {
            $method = substr($method, strlen($this->exposedkey));
        }
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $arguments);
        }
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
    }
}

class navigation_exposed_moodle_page extends \moodle_page {
    public function set_navigation(navigation_node $node) {
        $this->_navigation = $node;
    }
}

/**
 * This is a dummy object that allows us to call protected methods within the
 * global navigation class by prefixing the methods with `exposed_`.
 */
class exposed_settings_navigation extends settings_navigation {
    protected $exposedkey = 'exposed_';
    public function __construct() {
        global $PAGE;
        parent::__construct($PAGE);
    }
    public function __call($method, $arguments) {
        if (strpos($method, $this->exposedkey) !== false) {
            $method = substr($method, strlen($this->exposedkey));
        }
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $arguments);
        }
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit