[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

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