Changeset 5942


Ignore:
Timestamp:
01/18/12 02:34:26 (4 months ago)
Author:
vain
Message:
  • fixes for router buildURL() + unittest
  • improved function array_unequal_combine()
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/functions.core.php

    r5901 r5942  
    484484        $index = 0; 
    485485 
     486        # more keys than values, reduce keys array 
     487        while(count($keyArray) > count($valueArray)) 
     488        { 
     489            array_pop($keyArray); 
     490        } 
     491 
     492        # @todo more values than keys ? 
     493        # add pseudo keys a la "key-0" 
     494 
    486495        foreach($keyArray as $key) 
    487496        { 
    488             if(isset($valueArray[$index]) === true) 
    489             { 
     497            if(isset($valueArray[$index])) 
     498            { 
     499                # index is used, then incremented for the next turn in foreach (post-increment-operator) 
    490500                $returnArray[$key] = $valueArray[$index++]; 
    491501            } 
  • trunk/core/router.core.php

    r5937 r5942  
    224224    { 
    225225        # if urlstring is already a qualified url (http://...) 
    226         if(false !== strpos($urlstring, WWW_ROOT . 'index.php?mod=')) 
     226        if(false !== strpos($urlstring, WWW_ROOT . 'index.php?')) 
    227227        { 
    228228            # there is no need to rebuild it, just return 
    229229            return $urlstring; 
     230        } 
     231        elseif(false !== strpos($urlstring, 'index.php?')) 
     232        { 
     233            # there is no need to rebuild it, just the http prefix is missing 
     234            # add that and return 
     235            return WWW_ROOT . $urlstring; 
    230236        } 
    231237        # e.g. ROOT/news/admin 
     
    237243        else # ROOT/index.php?mod=abc&action=123&etc... 
    238244        { 
     245            # get only the part after "index.php=?" 
     246            if(false !== strpos($urlstring, 'index.php?')) 
     247            { 
     248                $urlstring = strstr($urlstring, 'index.php?'); 
     249            } 
     250 
    239251            # remove all double slahes 
    240252            while (false !== strpos($urlstring, '//')) 
    241253            { 
    242                 $url = str_replace('//', '/', $urlstring); 
    243             } 
    244  
    245             # get only the part after "index.php=?" 
    246             if(false !== strpos($urlstring, 'index.php?')) 
    247             { 
    248                 $urlstring = strstr($urlstring, 'index.php?'); 
    249             } 
    250  
    251             # and explode the string into an indexed array 
    252             $urlstring = ltrim($urlstring, '/'); 
     254                $urlstring = str_replace('//', '/', $urlstring); 
     255            } 
     256 
     257            # remove space and slashes from begin and end of string 
     258            $urlstring = trim($urlstring, ' /'); 
     259 
     260            # so something like "news/show/2" is left 
     261            # explode the string into an indexed array 
    253262            $url_params_idx_array = explode('/', $urlstring); 
    254  
    255             var_dump($url_params_idx_array); 
    256263 
    257264            /** 
     
    259266             * [0]=> "news"  to  [mod]    => "news" 
    260267             * [1]=> "show"  to  [action] => "show" 
     268             * [2]=> "2"     to  [id]     => "2" 
    261269             * 
    262270             * It also a static whitelist for url parameter keys. 
     
    266274             * Then a reverse lookup in the routes table. For now this is static. 
    267275             */ 
    268             if($url_params_idx_array[1] === 'admin') 
     276            if(isset($url_params_idx_array[1]) and $url_params_idx_array[1] === 'admin') 
    269277            { 
    270278                # module admin whitelist 
     
    278286 
    279287            $url_data = Clansuite_Functions::array_unequal_combine($url_keys, $url_params_idx_array); 
    280             $url = ''; 
    281288 
    282289            # Defaults to & for internal usage in html documents. 
    283             #  = ROOT/index.php?mod=abc&action=123&etc... 
    284             if($internal_url === true) 
    285             { 
    286                 $url = http_build_query($url_data, '', '&'); 
    287             } 
    288             # external link / redirect etc. 
    289             #  = ROOT/index.php?mod=abc&action=123&etc... 
    290             elseif($internal_url === false) 
    291             { 
    292                 $url = http_build_query($url_data, '', '&'); 
    293             } 
     290            $arg_separator = ($internal_url === true) ? '&' : '&'; 
     291 
     292            $url = http_build_query($url_data, '', $arg_separator); 
    294293 
    295294            #Clansuite_Debug::printR(WWW_ROOT . 'index.php?' . $url); 
  • trunk/core/targetroute.core.php

    r5519 r5942  
    367367        ); 
    368368 
    369         self::$parameters = array_merge(self::$parameters, $reset_params); 
     369        #self::$parameters = array_merge(self::$parameters, $reset_params); 
     370        self::$parameters = $reset_params; 
    370371    } 
    371372 
  • trunk/tests/runAllTests.php

    r5864 r5942  
    3838require_once 'simpletest/reporter.php'; 
    3939# hmm, we need to load this, to get rid of the "No runnable test cases in runAlltest" error 
    40 require_once 'simpletest/autorun.php'; 
     40#require_once 'simpletest/autorun.php'; 
    4141 
    4242# setup our testsuite and reporter 
  • trunk/tests/unittests/core/router.core.php

    r5937 r5942  
    100100        $this->assertEqual(WWW_ROOT . 'index.php?mod=news&action=show', $url); 
    101101 
     102        $urlstring = '/news/admin/edit/1'; 
     103        $internal_url = true; 
     104        $url = $this->router->buildURL($urlstring, $internal_url); 
     105        $this->assertEqual(WWW_ROOT . 'index.php?mod=news&sub=admin&action=edit&id=1', $url); 
     106 
    102107        /*$urlstring = ''; 
    103108        $internal_url = false; 
  • trunk/tests/unittests/core/viewhelper/form/form.core.php

    r5936 r5942  
    6363    { 
    6464        # set internal url - rebuilds the external url via router 
    65         $this->form->setAction('/module/action'); 
    66         $this->assertEqual( WWW_ROOT . 'index.php?mod=module&sub=action', $this->form->getAction()); 
     65        $this->form->setAction('/news/show'); 
     66        $this->assertEqual( WWW_ROOT . 'index.php?mod=news&action=show', $this->form->getAction()); 
    6767 
    6868        # set external url 
     69        $this->form->setAction(WWW_ROOT .'index.php?mod=news&action=show'); 
     70        $this->assertEqual( WWW_ROOT . 'index.php?mod=news&action=show', $this->form->getAction()); 
     71 
     72        # set external url withput www_root (http root) 
    6973        $this->form->setAction('index.php?mod=news&action=show'); 
    70         $this->assertEqual( WWW_ROOT . 'index.php?mod%3Dnews%26action%3Dshow', $this->form->getAction()); 
     74        $this->assertEqual( WWW_ROOT . 'index.php?mod=news&action=show', $this->form->getAction()); 
    7175    } 
    7276 
    7377    public function testGetAction() 
    7478    { 
    75         $this->form->setAction('index.php?mod=news&action=show'); 
    76  
    7779        # via getter - qualified url 
    78         $this->assertEqual( WWW_ROOT . 'index.php?mod%3Dnews%26action%3Dshow', $this->form->getAction()); 
     80        $url = WWW_ROOT . 'index.php?mod=news&action=show'; 
     81        $this->form->setAction( $url ); 
     82        $this->assertEqual( $url, $this->form->getAction()); 
    7983    } 
    8084 
Note: See TracChangeset for help on using the changeset viewer.