[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/Form/ -> DropdownElementTest.php (source)

   1  <?php
   2  
   3  namespace dokuwiki\test\Form;
   4  
   5  use dokuwiki\Form;
   6  use DOMWrap\Document;
   7  
   8  class DropdownElementTest extends \DokuWikiTest
   9  {
  10  
  11      function testDefaults()
  12      {
  13          $form = new Form\Form();
  14  
  15          // basic tests
  16          $options = array('first', 'second', 'third');
  17          $element = $form->addDropdown('foo', $options, 'label text');
  18          $this->assertEquals('first', $element->val());
  19          $element->val('second');
  20          $this->assertEquals('second', $element->val());
  21          $element->val('nope');
  22          $this->assertEquals('first', $element->val());
  23  
  24          // associative array
  25          $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
  26          $element->options($options);
  27          $this->assertEquals('first', $element->val());
  28          $element->val('second');
  29          $this->assertEquals('second', $element->val());
  30          $element->val('nope');
  31          $this->assertEquals('first', $element->val());
  32  
  33          // HTML
  34          $html = $form->toHTML();
  35          $pq = (new Document())->html($html);
  36  
  37          $select = $pq->find('select[name=foo]');
  38          $this->assertTrue($select->count() == 1);
  39  
  40          $options = $pq->find('option');
  41          $this->assertTrue($options->count() == 3);
  42  
  43          $option = $pq->find('option[selected=selected]');
  44          $this->assertTrue($option->count() == 1);
  45          $this->assertEquals('first', $option->attr('value'));
  46          $this->assertEquals('A first Label', $option->text());
  47  
  48          $label = $pq->find('label');
  49          $this->assertTrue($label->count() == 1);
  50          $this->assertEquals('label text', $label->find('span')->text());
  51      }
  52  
  53      function testExtendedOptions()
  54      {
  55          $form = new Form\Form();
  56  
  57          $options = array(
  58              'first' => array(
  59                  'label' => 'the label',
  60                  'attrs' => array(
  61                      'id' => 'theID',
  62                      'class' => 'two classes',
  63                      'data-foo' => 'bar',
  64                  ),
  65              ),
  66              'second',
  67              '3' => array(
  68                  'label' => 'the label of the complex third option',
  69              ),
  70          );
  71  
  72          $form->addDropdown('foo', $options, 'label text');
  73          // HTML
  74          $html = $form->toHTML();
  75          $pq = (new Document())->html($html);;
  76  
  77          $select = $pq->find('select[name=foo]');
  78          $this->assertTrue($select->count() == 1);
  79  
  80          $options = $pq->find('option');
  81          $this->assertEquals(3, $options->count());
  82  
  83          $option = $pq->find('option#theID');
  84          $this->assertEquals(1, $option->count());
  85          $this->assertEquals('first', $option->attr('value'));
  86          $this->assertEquals('the label', $option->text());
  87          $this->assertEquals('bar', $option->attr('data-foo'));
  88          $this->assertTrue($option->hasClass('two'));
  89          $this->assertTrue($option->hasClass('classes'));
  90      }
  91  
  92      public function testOptgroups()
  93      {
  94          $form = new Form\Form();
  95  
  96          $options1 = array(
  97              'first' => 'the label',
  98              'second',
  99          );
 100  
 101          $options2 = array(
 102              'third' => array(
 103                  'label' => 'label of third option',
 104                  'attribute' => 'attribute-value',
 105              ),
 106              'fourth',
 107          );
 108  
 109          $dropdown = $form->addDropdown('foo', null, 'label text');
 110          $dropdown->addOptGroup('opt1', $options1);
 111          $dropdown->addOptGroup('opt2', $options2);
 112  
 113          $dropdown->val('third');
 114          $this->assertEquals('third', $dropdown->val());
 115  
 116          /** @var Form\OptGroup[] $optGroups */
 117          $optGroups = $dropdown->optGroups();
 118          $this->assertEquals(array(
 119              'first' => array('label' => 'the label'),
 120              'second' => array('label' => 'second'),
 121          ), $optGroups['opt1']->options());
 122  
 123          // HTML
 124          $html = $form->toHTML();
 125          $pq = (new Document())->html($html);
 126  
 127          $optGroupsHTML = $pq->find('optgroup');
 128          $this->assertEquals(2, $optGroupsHTML->count());
 129  
 130          $options = $pq->find('option');
 131          $this->assertEquals(4, $options->count());
 132  
 133          $selected = $pq->find('option[selected=selected]');
 134          $this->assertEquals('third', $selected->attr('value'));
 135          $this->assertEquals('label of third option', $selected->text());
 136      }
 137  
 138      /**
 139       * Prevent double select that might occur because `'Auto' == 0` is true
 140       */
 141      public function testDoubleSelect()
 142      {
 143          $form = new Form\Form();
 144          $form->addDropdown('foo', ['Auto', 0, 1]);
 145  
 146          $html = $form->toHTML();
 147  
 148          $pq = (new Document())->html($html);
 149          $selected = $pq->find('option[selected=selected]');
 150          $this->assertEquals(1, $selected->count());
 151          $this->assertEquals('Auto', $selected->text());
 152      }
 153  
 154      /**
 155       * Ensure that there is always only a single one selected option
 156       */
 157      public function testOptgroupsDoubleselect()
 158      {
 159          $form = new Form\Form();
 160          $options1 = array(
 161              'double' => 'the label',
 162          );
 163  
 164          $options2 = array(
 165              'double' => array(
 166                  'label' => 'label of third option',
 167                  'attribute' => 'attribute-value',
 168              ),
 169          );
 170  
 171          $dropdown = $form->addDropdown('foo', null, 'label text');
 172          $dropdown->addOptGroup('opt1', $options1);
 173          $dropdown->addOptGroup('opt2', $options2);
 174          $dropdown->val('double');
 175  
 176          // HTML
 177          $html = $form->toHTML();
 178          $pq = (new Document())->html($html);
 179          $selected = $pq->find('option[selected=selected]');
 180          $this->assertEquals(1, $selected->count());
 181          $this->assertEquals('the label', $selected->text());
 182      }
 183  
 184      /**
 185       * check that posted values overwrite preset default
 186       */
 187      public function testPrefill()
 188      {
 189          global $INPUT;
 190          $INPUT->post->set('foo', 'second');
 191  
 192          $form = new Form\Form();
 193          $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
 194          $element = $form->addDropdown('foo', $options, 'label text')->val('third');
 195          $this->assertEquals('third', $element->val());
 196  
 197          $html = $form->toHTML();
 198          $pq = (new Document())->html($html);
 199  
 200          $option = $pq->find('option[selected=selected]');
 201          $this->assertTrue($option->count() == 1);
 202          $this->assertEquals('second', $option->attr('value'));
 203          $this->assertEquals('The second Label', $option->text());
 204      }
 205  
 206      public function testMultiple()
 207      {
 208          $form = new Form\Form();
 209  
 210          $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
 211          $element = $form->addDropdown('foo', $options, 'label text')->attr('multiple', '1');
 212  
 213          // only two of these values are valid
 214          $element->val(['first', 'third', 'fourth']);
 215          $this->assertEquals(['first', 'third'], $element->val());
 216  
 217          // check HTML
 218          $html = $form->toHTML();
 219          $pq = (new Document())->html($html);;
 220          $option = $pq->find('option[selected=selected]');
 221  
 222          $this->assertEquals('A first Label', $option->get(0)->textContent);
 223          $this->assertEquals('Just 3', $option->get(1)->textContent);
 224      }
 225  }