You are here

class FieldRawValueTest in Twig Field Value 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/FieldValue/FieldRawValueTest.php \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldRawValueTest

@coversDefaultClass \Drupal\twig_field_value\Twig\Extension\FieldValueExtension @group twig_field_value

Hierarchy

Expanded class hierarchy of FieldRawValueTest

File

tests/src/Unit/FieldValue/FieldRawValueTest.php, line 12

Namespace

Drupal\Tests\twig_field_value\Unit\FieldValue
View source
class FieldRawValueTest extends UnitTestCase {

  /**
   * The Twig extension under test.
   *
   * @var \Drupal\twig_field_value\Twig\Extension\FieldValueExtension
   */
  protected $extension;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->extension = new FieldValueExtension();
  }

  /**
   * Returns a mock FieldItem.
   *
   * @param mixed $values
   *
   * @return \Drupal\Core\Field\FieldItemBase
   */
  protected function mockFieldItem($values) {
    $field_item = $this
      ->getMockBuilder('Drupal\\Core\\Field\\FieldItemBase')
      ->disableOriginalConstructor()
      ->getMock();
    $field_item
      ->expects($this
      ->any())
      ->method('getValue')
      ->will($this
      ->returnValue($values));
    return $field_item;
  }

  /**
   * Asserts the twig field_raw filter.
   *
   * @dataProvider providerTestRawValues
   * @covers ::getRawValues
   *
   * @param $expected_result
   * @param $render_array
   * @param $key
   */
  public function testRawValues($expected_result, $render_array, $key) {
    $result = $this->extension
      ->getRawValues($render_array, $key);
    $this
      ->assertSame($expected_result, $result);
  }

  /**
   * Provides data and expected results for the test method.
   *
   * @return array
   *   Data and expected results.
   */
  public function providerTestRawValues() {
    return [
      // Invalid render arrays.
      [
        NULL,
        NULL,
        '',
      ],
      [
        NULL,
        [],
        '',
      ],
      [
        NULL,
        [
          '#theme' => 'field',
          '#no_items' => [],
        ],
        '',
      ],
      [
        NULL,
        [
          '#theme' => 'field',
          '#items' => [],
        ],
        '',
      ],
      [
        NULL,
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem(NULL),
        ],
        '',
      ],
      // Request all values, field with single value.
      [
        [
          'value' => 'text_value',
        ],
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'value' => 'text_value',
            ],
          ]),
        ],
        '',
      ],
      // Request all values, field with multiple values.
      [
        [
          'alt' => 'alt_value',
          'title' => 'title_value',
        ],
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'alt' => 'alt_value',
              'title' => 'title_value',
            ],
          ]),
        ],
        '',
      ],
      // Request 'foo', but value not exist.
      [
        NULL,
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'alt' => 'alt_value',
              'title' => 'title_value',
            ],
          ]),
        ],
        'foo',
      ],
      // Request 'alt' value, field cardinality = 1.
      [
        'alt_value',
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'alt' => 'alt_value',
              'title' => 'title_value',
            ],
          ]),
        ],
        'alt',
      ],
      // Request all values, field cardinality = 2.
      [
        [
          [
            'alt' => 'alt_value_1',
            'title' => 'title_value_1',
          ],
          [
            'alt' => 'alt_value_2',
            'title' => 'title_value_2',
          ],
        ],
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'alt' => 'alt_value_1',
              'title' => 'title_value_1',
            ],
            [
              'alt' => 'alt_value_2',
              'title' => 'title_value_2',
            ],
          ]),
        ],
        '',
      ],
      // Request 'alt' value, field cardinality = 2.
      [
        [
          'alt_value_1',
          'alt_value_2',
        ],
        [
          '#theme' => 'field',
          '#items' => $this
            ->mockFieldItem([
            [
              'alt' => 'alt_value_1',
              'title' => 'title_value_1',
            ],
            [
              'alt' => 'alt_value_2',
              'title' => 'title_value_2',
            ],
          ]),
        ],
        'alt',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldRawValueTest::$extension protected property The Twig extension under test.
FieldRawValueTest::mockFieldItem protected function Returns a mock FieldItem.
FieldRawValueTest::providerTestRawValues public function Provides data and expected results for the test method.
FieldRawValueTest::setUp protected function Overrides UnitTestCase::setUp
FieldRawValueTest::testRawValues public function Asserts the twig field_raw filter.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.