public function SchemaMetatagTagsTestBase::testTagsInputOutput in Schema.org Metatag 7
Confirm that tags can be saved and that the output of each tag is correct.
File
- tests/
schema_metatag.base.test, line 60
Class
- SchemaMetatagTagsTestBase
- Base class for testing a module's custom tags.
Code
public function testTagsInputOutput() {
if (empty($this->schemaTags)) {
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
->assertResponse(200);
$this
->assertTrue('//input[@type="submit"][@value="Save"]');
// Configure all the tag values and post the results.
$expected_output_values = $form_values = [];
foreach ($this->schemaTags as $tag_name => $class) {
// Transform the tag_name to the camelCase key used in the form.
$key = $this
->getKey($tag_name);
// Find the name of the class that defines this property, 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.
$test_value = $class::testValue();
// Adjust the input value as necessary to transform it to the
// expected output value, and store that.
$processed_value = $class::processedTestValue($test_value);
$expected_output_values[$key] = $class::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.
$buffer = '][';
$prefix = 'metatags[und][';
$suffix = '][value]';
$start = '[';
$end = ']';
if (!is_array($test_value)) {
$form_values[$prefix . $tag_name . $suffix] = $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) {
$keys = implode('][', [
$key,
$key2,
$key3,
$key4,
]);
$form_values[$prefix . $tag_name . $suffix . $start . $keys . $end] = $value4;
}
}
else {
$keys = implode('][', [
$key,
$key2,
$key3,
]);
$form_values[$prefix . $tag_name . $suffix . $start . $keys . $end] = $value3;
}
}
}
else {
$keys = implode('][', [
$key,
$key2,
]);
$form_values[$prefix . $tag_name . $suffix . $start . $keys . $end] = $value2;
}
}
}
else {
$keys = implode('][', [
$key,
]);
$form_values[$prefix . $tag_name . $suffix . $start . $keys . $end] = $value;
}
}
}
}
$this
->drupalPost(NULL, $form_values, 'Save');
$this
->assertText($save_message);
// Load the config page to confirm the settings got saved.
$this
->drupalGet($config_path);
$this
->assertResponse(200);
cache_clear_all();
// Load the rendered page to see if the JSON-LD is displayed correctly.
$this
->drupalGet($rendered_path);
$this
->assertResponse(200);
// Make sure JSON-LD is present and can be decoded.
$elements = $this
->xpath('//script[@type="application/ld+json"]');
$this
->assertEqual(count($elements), 1);
$json = json_decode($elements[0], TRUE);
$this
->assertTrue($json);
$output_values = $json['@graph'][0];
// Compare input and output values.
foreach ($this->schemaTags as $tag_name => $class) {
$key = $this
->getKey($tag_name);
$this
->assertEqual($output_values[$key], $expected_output_values[$key]);
}
}
$this
->drupalLogout();
}