public function PluginBaseUnitTest::testUnpackOptions

Tests the unpackOptions method.

See also

\Drupal\views\Plugin\views\PluginBase::unpackOptions.

File

drupal/core/modules/views/lib/Drupal/views/Tests/PluginBaseUnitTest.php, line 33
Contains \Drupal\views\Tests\PluginBaseUnitTest.

Class

PluginBaseUnitTest
Tests code of the views plugin base class.

Namespace

Drupal\views\Tests

Code

public function testUnpackOptions() {
  $plugin = $this
    ->getTestPlugin();
  $test_parameters = array();

  // Set a storage but no value, so the storage value should be kept.
  $test_parameters[] = array(
    'storage' => array(
      'key' => 'value',
    ),
    'options' => array(),
    'definition' => array(
      'key' => array(
        'default' => 'value2',
      ),
    ),
    'expected' => array(
      'key' => 'value',
    ),
  );

  // Set a storage and a option value, so the option value should be kept.
  $test_parameters[] = array(
    'storage' => array(
      'key' => 'value',
    ),
    'options' => array(
      'key' => 'value2',
    ),
    'definition' => array(
      'key' => array(
        'default' => 'value3',
      ),
    ),
    'expected' => array(
      'key' => 'value2',
    ),
    '',
  );

  // Set no storage but an options value, so the options value should be kept.
  $test_parameters[] = array(
    'options' => array(
      'key' => 'value',
    ),
    'definition' => array(
      'key' => array(
        'default' => 'value2',
      ),
    ),
    'expected' => array(
      'key' => 'value',
    ),
  );

  // Set additional options, which aren't part of the definition, so they
  // should be ignored if all is set.
  $test_parameters[] = array(
    'options' => array(
      'key' => 'value',
      'key2' => 'value2',
    ),
    'definition' => array(
      'key' => array(
        'default' => 'value2',
      ),
    ),
    'expected' => array(
      'key' => 'value',
    ),
  );
  $test_parameters[] = array(
    'options' => array(
      'key' => 'value',
      'key2' => 'value2',
    ),
    'definition' => array(
      'key' => array(
        'default' => 'value2',
      ),
    ),
    'expected' => array(
      'key' => 'value',
      'key2' => 'value2',
    ),
    'all' => TRUE,
  );

  // Provide multiple options with their corresponding definition.
  $test_parameters[] = array(
    'options' => array(
      'key' => 'value',
      'key2' => 'value2',
    ),
    'definition' => array(
      'key' => array(
        'default' => 'value2',
      ),
      'key2' => array(
        'default' => 'value3',
      ),
    ),
    'expected' => array(
      'key' => 'value',
      'key2' => 'value2',
    ),
  );

  // Set a complex definition structure with a zero and a one level structure.
  $test_parameters[] = array(
    'options' => array(
      'key0' => 'value',
      'key1' => array(
        'key1:1' => 'value1',
        'key1:2' => 'value2',
      ),
    ),
    'definition' => array(
      'key0' => array(
        'default' => 'value0',
      ),
      'key1' => array(
        'contains' => array(
          'key1:1' => array(
            'default' => 'value1:1',
          ),
        ),
      ),
    ),
    'expected' => array(
      'key0' => 'value',
      'key1' => array(
        'key1:1' => 'value1',
      ),
    ),
  );

  // Setup a two level structure.
  $test_parameters[] = array(
    'options' => array(
      'key2' => array(
        'key2:1' => array(
          'key2:1:1' => 'value0',
          'key2:1:2' => array(
            'key2:1:2:1' => 'value1',
          ),
        ),
      ),
    ),
    'definition' => array(
      'key2' => array(
        'contains' => array(
          'key2:1' => array(
            'contains' => array(
              'key2:1:1' => array(
                'default' => 'value2:1:2:1',
              ),
              'key2:1:2' => array(
                'contains' => array(
                  'key2:1:2:1' => array(
                    'default' => 'value2:1:2:1',
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
    'expected' => array(
      'key2' => array(
        'key2:1' => array(
          'key2:1:1' => 'value0',
          'key2:1:2' => array(
            'key2:1:2:1' => 'value1',
          ),
        ),
      ),
    ),
  );
  foreach ($test_parameters as $parameter) {
    $parameter += array(
      'storage' => array(),
    );
    $plugin
      ->unpackOptions($parameter['storage'], $parameter['options'], $parameter['definition'], !empty($parameter['all']));
    $this
      ->assertEqual($parameter['storage'], $parameter['expected']);
  }
}