You are here

public function SchemaMetatagTagsTestBase::testTagsInputOutput in Schema.org Metatag 8.2

Same name and namespace in other branches
  1. 8 tests/src/Functional/SchemaMetatagTagsTestBase.php \Drupal\Tests\schema_metatag\Functional\SchemaMetatagTagsTestBase::testTagsInputOutput()

Confirm that tags can be saved and that the output of each tag is correct.

File

tests/src/Functional/SchemaMetatagTagsTestBase.php, line 189

Class

SchemaMetatagTagsTestBase
Base class to test all of the meta tags that are in a specific module.

Namespace

Drupal\Tests\schema_metatag\Functional

Code

public function testTagsInputOutput() {
  if (empty($this->propertyTypes)) {
    $this
      ->markTestSkipped('Not enough information to test.');
    return;
  }
  $paths = $this
    ->getPaths();
  foreach ($paths as $item) {
    list($config_path, $rendered_path, $save_message) = $item;

    // Load the config page.
    $this
      ->drupalGet($config_path);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@type="submit"][@value="Save"]');

    // Configure all the tag values and post the results.
    $expected_output_values = $raw_values = $form_values = [];
    $form_values = [];
    foreach ($this->propertyTypes as $tag_name => $property_type) {

      // Transform the tag_name to the camelCase key used in the form.
      $key = $this
        ->getKey($tag_name);

      // Find the name of the property type and use it to
      // identify a valid test value, and determine what the rendered output
      // should look like. Store the rendered value so we can compare it to
      // the output. Store the raw value so we can check that it exists in the
      // config form.
      $property_plugin = $this
        ->propertyTypeManager()
        ->createInstance($property_type);
      $type = array_key_exists($tag_name, $this->treeParent) ? $this->treeParent[$tag_name] : $property_plugin
        ->getTreeParent();
      $test_type = is_array($type) ? array_shift($type) : $type;
      $test_value = $property_plugin
        ->testValue($test_type);

      // Store the input value.
      $raw_values[$tag_name] = $test_value;

      // Adjust the input value as necessary to transform it to the
      // expected output value, and store that.
      $processed_value = $property_plugin
        ->processedTestValue($test_value);
      $expected_output_values[$key] = $property_plugin
        ->outputValue($processed_value);

      // Rewrite the test values to match the way the form elements are
      // structured.
      // @TODO There is probably some way to write this as a recursive
      // function that will go more than three levels deep, but for now this
      // is enough.
      if (!is_array($test_value)) {
        $form_values[$tag_name] = $test_value;
      }
      else {
        foreach ($test_value as $key => $value) {
          if (is_array($value)) {
            foreach ($value as $key2 => $value2) {
              if (is_array($value2)) {
                foreach ($value2 as $key3 => $value3) {
                  if (is_array($value3)) {
                    foreach ($value3 as $key4 => $value4) {
                      $keystring = implode('][', [
                        $key,
                        $key2,
                        $key3,
                        $key4,
                      ]);
                      $form_values[$tag_name . '[' . $keystring . ']'] = $value4;
                    }
                  }
                  else {
                    $keystring = implode('][', [
                      $key,
                      $key2,
                      $key3,
                    ]);
                    $form_values[$tag_name . '[' . $keystring . ']'] = $value3;
                  }
                }
              }
              else {
                $keystring = implode('][', [
                  $key,
                  $key2,
                ]);
                $form_values[$tag_name . '[' . $keystring . ']'] = $value2;
              }
            }
          }
          else {
            $keystring = implode('][', [
              $key,
            ]);
            $form_values[$tag_name . '[' . $keystring . ']'] = $value;
          }
        }
      }
    }
    $this
      ->drupalPostForm(NULL, $form_values, 'Save');
    $this
      ->assertSession()
      ->pageTextContains($save_message, 'Configuration successfully posted.');

    // Load the config page to confirm the settings got saved.
    $this
      ->drupalGet($config_path);
    foreach ($this->propertyTypes as $tag_name => $property_type) {

      // Check that simple string test values exist in the form to see that
      // form values were saved accurately. Don't try to recurse through all
      // arrays, more complicated values will be tested from the JSON output
      // they create.
      if (is_string($raw_values[$tag_name])) {
        $string = strtr('//*[@name=":tag_name"]', [
          ':tag_name' => $tag_name,
        ]);
        $elements = $this
          ->xpath($string);
        $value = count($elements) ? $elements[0]
          ->getValue() : NULL;
        $this
          ->assertEquals($value, $raw_values[$tag_name]);
      }
    }

    // Load the rendered page to see if the JSON-LD is displayed correctly.
    $this
      ->drupalGet($rendered_path);
    $this
      ->assertSession()
      ->statusCodeEquals(200);

    // Make sure JSON-LD is present and can be decoded.
    $this
      ->assertSession()
      ->elementExists('xpath', '//script[@type="application/ld+json"]');
    $elements = $this
      ->xpath('//script[@type="application/ld+json"]');
    $this
      ->assertEquals(count($elements), 1);
    $json = json_decode($elements[0]
      ->getHtml(), TRUE);
    $this
      ->assertNotEmpty($json);
    $output_values = $json['@graph'][0];

    // Compare input and output values.
    foreach ($this->propertyTypes as $tag_name => $property_type) {
      $key = $this
        ->getKey($tag_name);
      $this
        ->assertEquals($output_values[$key], $expected_output_values[$key]);
    }
  }
  $this
    ->drupalLogout();
}