Commit c5db1d89 authored by citlacom's avatar citlacom Committed by tim.plunkett
Browse files

Issue #1637672 by citlacom | Pedro Lozano: Convert field tests to PSR-0.

parent 536d0c8b
<?php
/**
* @file
* Definition of Drupal\views\Test\Field\ApiDataTest.
*/
namespace Drupal\views\Tests\Field;
use Drupal\simpletest\WebTestBase;
use ViewsSqlTest;
/**
* Test the produced views_data.
*/
class ApiDataTest extends ApiTestBase {
/**
* Stores the fields for this test case.
*/
var $fields;
public static function getInfo() {
return array(
'name' => 'Fieldapi: Views Data',
'description' => 'Tests the fieldapi views data.',
'group' => 'Views Modules',
);
}
function setUp() {
parent::setUp();
$langcode = LANGUAGE_NOT_SPECIFIED;
$field_names = $this->setUpFields();
// The first one will be attached to nodes only.
$instance = array(
'field_name' => $field_names[0],
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
// The second one will be attached to users only.
$instance = array(
'field_name' => $field_names[1],
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
// The third will be attached to both nodes and users.
$instance = array(
'field_name' => $field_names[2],
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
$instance = array(
'field_name' => $field_names[2],
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
// Now create some example nodes/users for the view result.
for ($i = 0; $i < 5; $i++) {
$edit = array(
// @TODO Write a helper method to create such values.
'field_name_0' => array($langcode => array((array('value' => $this->randomName())))),
'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
);
$this->nodes[] = $this->drupalCreateNode($edit);
}
$permissions = array('access comments', 'access content', 'post comments', 'skip comment approval');
for ($i = 0; $i < 5; $i++) {
$account = $this->drupalCreateUser($permissions);
$account->field_name_1 = array($langcode => array((array('value' => $this->randomName()))));
$account->field_name_2 = array($langcode => array((array('value' => $this->randomName()))));
$account->save();
$this->users[] = $account;
}
// Reset views data cache.
$cache = &drupal_static('_views_fetch_data' . '_cache');
$recursion_protection = &drupal_static('_views_fetch_data' . '_recursion_protected');
$cache = NULL;
$recursion_protection = NULL;
}
/**
* Unit testing the views data structure.
*
* We check data structure for both node and node revision tables.
*/
function testViewsData() {
$data = views_fetch_data();
// Check the table and the joins of the first field.
// Attached to node only.
$field = $this->fields[0];
$current_table = _field_sql_storage_tablename($field);
$revision_table = _field_sql_storage_revision_tablename($field);
$this->assertTrue(isset($data[$current_table]));
$this->assertTrue(isset($data[$revision_table]));
// The node field should join against node.
$this->assertTrue(isset($data[$current_table]['table']['join']['node']));
$this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
$expected_join = array(
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
),
);
$this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
$expected_join = array(
'left_field' => 'vid',
'field' => 'revision_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
),
);
$this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
// Check the table and the joins of the second field.
// Attached to both node and user.
$field_2 = $this->fields[2];
$current_table_2 = _field_sql_storage_tablename($field_2);
$revision_table_2 = _field_sql_storage_revision_tablename($field_2);
$this->assertTrue(isset($data[$current_table_2]));
$this->assertTrue(isset($data[$revision_table_2]));
// The second field should join against both node and users.
$this->assertTrue(isset($data[$current_table_2]['table']['join']['node']));
$this->assertTrue(isset($data[$revision_table_2]['table']['join']['node_revision']));
$this->assertTrue(isset($data[$current_table_2]['table']['join']['users']));
$expected_join = array(
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['node']);
$expected_join = array(
'left_field' => 'vid',
'field' => 'revision_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$revision_table_2]['table']['join']['node_revision']);
$expected_join = array(
'left_field' => 'uid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'user'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['users']);
// Check the fields
// @todo
// Check the arguments
// @todo
// Check the sort criterias
// @todo
// Check the relationships
// @todo
}
}
<?php
/**
* @file
* Definition of Drupal\views\Test\Field\ApiTestHelper.
*/
/**
* @TODO
* - Test on a generic entity not on a node.
*
* What has to be tested:
* - Take sure that every wanted field is added to the according entity type.
* - Take sure the joins are done correct.
* - Use basic fields and take sure that the full wanted object is build.
* - Use relationships between different entity types, for example node and the node author(user).
*/
namespace Drupal\views\Tests\Field;
use Drupal\simpletest\WebTestBase;
use ViewsSqlTest;
/**
* Provides some helper methods for testing fieldapi integration into views.
*/
class ApiTestBase extends ViewsSqlTest {
protected $profile = 'standard';
/**
* Stores the field definitions used by the test.
* @var array
*/
public $fields;
/**
* Stores the instances of the fields. They have
* the same keys as the fields.
* @var array
*/
public $instances;
function setUpFields($amount = 3) {
// Create three fields.
$field_names = array();
for ($i = 0; $i < $amount; $i++) {
$field_names[$i] = 'field_name_' . $i;
$field = array('field_name' => $field_names[$i], 'type' => 'text');
$this->fields[$i] = $field = field_create_field($field);
}
return $field_names;
}
function setUpInstances($bundle = 'page') {
foreach ($this->fields as $key => $field) {
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'node',
'bundle' => 'page',
);
$this->instances[$key] = field_create_instance($instance);
}
}
/**
* Clear all views caches and static caches which are required for the patch.
*/
function clearViewsCaches() {
// Reset views data cache.
$cache = &drupal_static('_views_fetch_data' . '_cache');
$recursion_protection = &drupal_static('_views_fetch_data' . '_recursion_protected');
$cache = NULL;
$recursion_protection = NULL;
}
}
......@@ -2,251 +2,14 @@
/**
* @file
* Tests the fieldapi integration of viewsdata.
* Definition of Drupal\views\Test\Field\HandlerFieldFieldTest.
*/
use Drupal\views\View;
/**
* @TODO
* - Test on a generic entity not on a node.
*
* What has to be tested:
* - Take sure that every wanted field is added to the according entity type.
* - Take sure the joins are done correct.
* - Use basic fields and take sure that the full wanted object is build.
* - Use relationships between different entity types, for example node and the node author(user).
*/
/**
* Provides some helper methods for testing fieldapi integration into views.
*/
class ViewsFieldApiTestHelper extends ViewsSqlTest {
protected $profile = 'standard';
/**
* Stores the field definitions used by the test.
* @var array
*/
public $fields;
/**
* Stores the instances of the fields. They have
* the same keys as the fields.
* @var array
*/
public $instances;
function setUpFields($amount = 3) {
// Create three fields.
$field_names = array();
for ($i = 0; $i < $amount; $i++) {
$field_names[$i] = 'field_name_' . $i;
$field = array('field_name' => $field_names[$i], 'type' => 'text');
namespace Drupal\views\Tests\Field;
$this->fields[$i] = $field = field_create_field($field);
}
return $field_names;
}
function setUpInstances($bundle = 'page') {
foreach ($this->fields as $key => $field) {
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'node',
'bundle' => 'page',
);
$this->instances[$key] = field_create_instance($instance);
}
}
/**
* Clear all views caches and static caches which are required for the patch.
*/
function clearViewsCaches() {
// Reset views data cache.
$cache = &drupal_static('_views_fetch_data' . '_cache');
$recursion_protection = &drupal_static('_views_fetch_data' . '_recursion_protected');
$cache = NULL;
$recursion_protection = NULL;
}
}
/**
* Test the produced views_data.
*/
class viewsFieldApiDataTest extends ViewsFieldApiTestHelper {
/**
* Stores the fields for this test case.
*/
var $fields;
public static function getInfo() {
return array(
'name' => 'Fieldapi: Views Data',
'description' => 'Tests the fieldapi views data.',
'group' => 'Views Modules',
);
}
function setUp() {
parent::setUp();
$langcode = LANGUAGE_NOT_SPECIFIED;
$field_names = $this->setUpFields();
// The first one will be attached to nodes only.
$instance = array(
'field_name' => $field_names[0],
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
// The second one will be attached to users only.
$instance = array(
'field_name' => $field_names[1],
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
// The third will be attached to both nodes and users.
$instance = array(
'field_name' => $field_names[2],
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
$instance = array(
'field_name' => $field_names[2],
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
// Now create some example nodes/users for the view result.
for ($i = 0; $i < 5; $i++) {
$edit = array(
// @TODO Write a helper method to create such values.
'field_name_0' => array($langcode => array((array('value' => $this->randomName())))),
'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
);
$this->nodes[] = $this->drupalCreateNode($edit);
}
$permissions = array('access comments', 'access content', 'post comments', 'skip comment approval');
for ($i = 0; $i < 5; $i++) {
$account = $this->drupalCreateUser($permissions);
$account->field_name_1 = array($langcode => array((array('value' => $this->randomName()))));
$account->field_name_2 = array($langcode => array((array('value' => $this->randomName()))));
$account->save();
$this->users[] = $account;
}
// Reset views data cache.
$cache = &drupal_static('_views_fetch_data' . '_cache');
$recursion_protection = &drupal_static('_views_fetch_data' . '_recursion_protected');
$cache = NULL;
$recursion_protection = NULL;
}
/**
* Unit testing the views data structure.
*
* We check data structure for both node and node revision tables.
*/
function testViewsData() {
$data = views_fetch_data();
// Check the table and the joins of the first field.
// Attached to node only.
$field = $this->fields[0];
$current_table = _field_sql_storage_tablename($field);
$revision_table = _field_sql_storage_revision_tablename($field);
$this->assertTrue(isset($data[$current_table]));
$this->assertTrue(isset($data[$revision_table]));
// The node field should join against node.
$this->assertTrue(isset($data[$current_table]['table']['join']['node']));
$this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
$expected_join = array(
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
),
);
$this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
$expected_join = array(
'left_field' => 'vid',
'field' => 'revision_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
),
);
$this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
// Check the table and the joins of the second field.
// Attached to both node and user.
$field_2 = $this->fields[2];
$current_table_2 = _field_sql_storage_tablename($field_2);
$revision_table_2 = _field_sql_storage_revision_tablename($field_2);
$this->assertTrue(isset($data[$current_table_2]));
$this->assertTrue(isset($data[$revision_table_2]));
// The second field should join against both node and users.
$this->assertTrue(isset($data[$current_table_2]['table']['join']['node']));
$this->assertTrue(isset($data[$revision_table_2]['table']['join']['node_revision']));
$this->assertTrue(isset($data[$current_table_2]['table']['join']['users']));
$expected_join = array(
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['node']);
$expected_join = array(
'left_field' => 'vid',
'field' => 'revision_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'node'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$revision_table_2]['table']['join']['node_revision']);
$expected_join = array(
'left_field' => 'uid',
'field' => 'entity_id',
'extra' => array(
array('field' => 'entity_type', 'value' => 'user'),
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
)
);
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['users']);
// Check the fields
// @todo
// Check the arguments
// @todo
// Check the sort criterias
// @todo
// Check the relationships
// @todo
}
}
use Drupal\simpletest\WebTestBase;
use ViewsSqlTest;
use Drupal\views\View;
/**
* Tests the field_field handler.
......@@ -256,7 +19,7 @@ function testViewsData() {
* Check locale:disabled, locale:enabled and locale:enabled with another language
* Check revisions
*/
class viewsHandlerFieldFieldTest extends ViewsFieldApiTestHelper {
class HandlerFieldFieldTest extends ApiTestBase {
public $nodes;
public static function getInfo() {
......@@ -440,7 +203,7 @@ public function _testMultipleFieldRender() {
}
protected function getFieldView() {
$view = new View();
$view = new View;
$view->name = 'view_fieldapi';
$view->description = '';
$view->tag = 'default';
......@@ -472,4 +235,3 @@ protected function getFieldView() {
}
}
......@@ -247,7 +247,6 @@ files[] = tests/test_plugins/views_test_plugin_access_test_dynamic.inc
files[] = tests/test_plugins/views_test_plugin_access_test_static.inc
files[] = tests/plugins/views_plugin_display.test
files[] = tests/views_access.test
files[] = tests/field/views_fieldapi.test
files[] = tests/views_groupby.test
files[] = tests/views_module.test
files[] = tests/views_plugin_localization_test.inc
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment