[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/inc/ -> pageutils_clean_id.test.php (source)

   1  <?php
   2  
   3  class init_clean_id_test extends DokuWikiTest
   4  {
   5  
   6      /** @inheritDoc */
   7      function teardown()
   8      {
   9          global $cache_cleanid;
  10          $cache_cleanid = array();
  11      }
  12  
  13      /**
  14       * DataProvider
  15       *
  16       * @return Generator|array
  17       * @see testCleanId
  18       */
  19      public function provideTestData()
  20      {
  21          // input, expected, optional options array
  22          $tests = [
  23              ['page', 'page'],
  24              ['pa_ge', 'pa_ge'],
  25              ['pa%ge', 'pa_ge'],
  26              ['pa#ge', 'pa_ge'],
  27              ['pàge', 'page'],
  28              ['pagĖ', 'page'],
  29              ['pa$%^*#ge', 'pa_ge'],
  30              ['*page*', 'page'],
  31              ['ښ', 'ښ'],
  32              ['päge', 'paege'],
  33              ['foo bar', 'foo_bar'],
  34              ['PÄGÖ', 'paegoe'],
  35              ['Faß', 'fass'],
  36              ['ښ侧化并곦  β', 'ښ侧化并곦_β'],
  37              ['page:page', 'page:page'],
  38              ['page;page', 'page:page'],
  39              ['page:page 1.2', 'page:page_1.2'],
  40              ['page._#!', 'page'],
  41              ['._#!page', 'page'],
  42              ['page._#!page', 'page._page'],
  43              ['ns._#!:page', 'ns:page'],
  44              ['ns:._#!page', 'ns:page'],
  45              ['ns._#!ns:page', 'ns._ns:page'],
  46              ['ns_:page', 'ns:page'],
  47              ['page...page', 'page...page'],
  48              ['page---page', 'page---page'],
  49              ['page___page', 'page_page'],
  50              ['page_-.page', 'page_-.page'],
  51              [':page', 'page'],
  52              [':ns:page', 'ns:page'],
  53              ['page:', 'page'],
  54              ['ns:page:', 'ns:page'],
  55  
  56              // use-slash handling
  57              ['page/page', 'page_page', ['useslash' => 0]],
  58              ['page/page', 'page:page', ['useslash' => 1]],
  59  
  60              // different sep-char
  61              ['pa-ge', 'pa-ge', ['sepchar' => '-']],
  62              ['pa%ge', 'pa-ge', ['sepchar' => '-']],
  63  
  64              // no deaccenting
  65              ['pàge', 'pàge', ['deaccent' => 0]],
  66              ['pagĖ', 'pagė', ['deaccent' => 0]],
  67              ['pagĒēĔĕĖėĘęĚě', 'pagēēĕĕėėęęěě', ['deaccent' => 0]],
  68              ['ښ', 'ښ', ['deaccent' => 0]],
  69              ['ښ侧化并곦ঝഈ', 'ښ侧化并곦ঝഈ', ['deaccent' => 0]],
  70  
  71              // romanize
  72              ['pàge', 'page', ['deaccent' => 2]],
  73              ['pagĖ', 'page', ['deaccent' => 2]],
  74              ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 2]],
  75              ['ښ', 'ښ', ['deaccent' => 2]],
  76              ['ښ侧化并곦ঝഈ', 'ښ侧化并곦ঝഈ', ['deaccent' => 2]],
  77  
  78              // deaccent and force ascii
  79              ['pàge', 'page', ['deaccent' => 1, 'ascii' => true]],
  80              ['pagĖ', 'page', ['deaccent' => 1, 'ascii' => true]],
  81              ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 1, 'ascii' => true]],
  82              ['ښ', '', ['deaccent' => 1, 'ascii' => true]],
  83              ['ښ侧化并곦ঝഈ', '', ['deaccent' => 1, 'ascii' => true]],
  84  
  85              // romanize and force ascii
  86              ['pàge', 'page', ['deaccent' => 2, 'ascii' => true]],
  87              ['pagĖ', 'page', ['deaccent' => 2, 'ascii' => true]],
  88              ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 2, 'ascii' => true]],
  89              ['ښ', '', ['deaccent' => 2, 'ascii' => true]],
  90              ['ښ侧化并곦ঝഈ', '', ['deaccent' => 2, 'ascii' => true]],
  91          ];
  92  
  93          foreach ($tests as $test) {
  94              // defaults
  95              $sepchar = $test[2]['sepchar'] ?: '_';
  96              $deaccent = isset($test[2]['deaccent']) ? $test[2]['deaccent'] : 1;
  97              $ascii = $test[2]['ascii'] ?: false;
  98  
  99              // unless set, test both useslash settings
 100              if (isset($test[2]['useslash'])) {
 101                  yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, $test[2]['useslash']]);
 102              } else {
 103                  yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, 0]);
 104                  yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, 1]);
 105              }
 106          }
 107      }
 108  
 109      /**
 110       * @dataProvider provideTestData
 111       * @param string $input
 112       * @param string $expected
 113       * @param bool $ascii
 114       * @param string $sepchar
 115       * @param int $deaccent
 116       * @param int $useslash
 117       */
 118      function testCleanId($input, $expected, $ascii, $sepchar, $deaccent, $useslash)
 119      {
 120          // set dokuwiki defaults
 121          global $conf;
 122          $conf['sepchar'] = $sepchar;
 123          $conf['deaccent'] = $deaccent;
 124          $conf['useslash'] = $useslash;
 125  
 126          $result = cleanID($input, $ascii);
 127          $this->assertEquals($expected, $result);
 128      }
 129  
 130  
 131  
 132      function test_caching_ascii()
 133      {
 134          global $conf;
 135          $conf['deaccent'] = 0;
 136          $this->assertEquals('pàge', cleanID('pàge', false));
 137          $this->assertEquals('page', cleanID('pàge', true));
 138  
 139          $this->assertEquals('page', cleanID('pagĖ', true));
 140          $this->assertEquals('pagė', cleanID('pagĖ', false));
 141      }
 142  
 143  }