- <?php
- * @file
- * Tests for Field Permission Example.
- *
- * @ingroup field_permission_example
- */
-
- * A generic field testing class.
- *
- * Subclass this one to test your specific field type
- * and get some basic unit testing for free.
- *
- * Since Simpletest only looks through one class definition
- * to find test functions, we define generic tests as
- * 'code_testWhatever' or 'form_testWhatever'. Subclasses
- * can then implement shim test methods that just call the
- * generic tests.
- *
- * 'code_' and 'form_' prefixes denote the type of test:
- * using code only, or through Backdrop page forms.
- *
- * @ingroup field_permission_example
- */
- class GenericFieldTest extends BackdropWebTestCase {
-
-
-
- protected $instanceNames;
-
-
- * Supply the field types we wish to test.
- *
- * Return an array of field types to instantiate and test.
- *
- * @return array
- * The field types we wish to use.
- */
- protected function getFieldTypes() {
- return array('these_are_not', 'valid_field_types', 'please_override');
- }
-
-
- * The module to enable.
- *
- * @return string
- * Module machine name.
- */
- protected function getModule() {
- return 'this-is-not-a-module-name-please-override';
- }
-
-
- * Simpletest's setUp().
- *
- * We want to be able to subclass this class, so we jump
- * through a few hoops in order to get the modules from args
- * and add our own.
- */
- public function setUp() {
- $this->instanceNames = array();
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules[] = 'node';
- $modules[] = 'field_ui';
- parent::setUp($modules);
- }
-
-
- * Verify that all required fields are specified in hook_field_info().
- *
- * The full list is label, description, settings, instance_settings,
- * default_widget, default_formatter, no_ui.
- *
- * Some are optional, and we won't check for those.
- *
- * In a sane world, this would be a unit test, rather than a
- * web test, but module_implements is unavailable to us
- * in unit tests.
- *
- * @see hook_field_info()
- */
- public function runTestGenericFieldInfo() {
- $field_types = $this->getFieldTypes();
- $module = $this->getModule();
- $info_keys = array(
- 'label',
- 'description',
- 'default_widget',
- 'default_formatter',
- );
-
-
-
-
- $modules = module_implements('field_info');
- $this->assertTrue(in_array($module, $modules),
- 'Module ' . $module . ' implements hook_field_info()');
-
- foreach ($field_types as $field_type) {
- $field_info = module_invoke($module, 'field_info');
- $this->assertTrue(isset($field_info[$field_type]),
- 'Module ' . $module . ' defines field type ' . $field_type);
- $field_info = $field_info[$field_type];
- foreach ($info_keys as $key) {
- $this->assertTrue(
- isset($field_info[$key]),
- $field_type . "'s " . $key . ' is set.'
- );
- }
- }
- }
-
-
- * Add all testable fields as instances to a content type.
- *
- * As a side-effect: Store the names of the instances created
- * in $this->$instance_names.
- *
- * @param object $node_type
- * A content type object. If none is provided, one will be generated.
- *
- * @return object
- * The content type object that has the fields attached.
- */
- public function codeTestGenericAddAllFields($node_type = NULL) {
- $this->instanceNames = array();
- if (!$node_type) {
- $node_type = $this->backdropCreateContentType();
- }
- foreach ($this->getFieldTypes() as $field_type) {
- $instance_name = backdrop_strtolower($this->randomName(32));
- $field = array(
- 'field_name' => $instance_name,
- 'type' => $field_type,
- );
- $field = field_create_field($field);
- $instance = array(
- 'field_name' => $instance_name,
- 'entity_type' => 'node',
- 'bundle' => $node_type->name,
- 'label' => backdrop_strtolower($this->randomName(20)),
- );
-
- $instance = field_create_instance($instance);
-
- _field_info_collate_fields(TRUE);
-
- $verify_instance = field_info_instance('node', $instance_name, $node_type->name);
- $this->assertTrue($verify_instance, 'Instance object exists.');
- $this->assertTrue(
- $verify_instance != NULL,
- 'field_info_instance() says ' . $instance_name . ' (' . $node_type->name . ') was created.'
- );
- $this->instanceNames[] = $instance_name;
- }
- return $node_type;
- }
-
-
- * Remove all fields in $this->field_names.
- *
- * @param mixed $node_type
- * A content type object. If none is specified,
- * the test fails.
- */
- public function codeTestGenericRemoveAllFields($node_type = NULL) {
- if (!$node_type) {
- $this->fail('No node type.');
- }
- if (count($this->instanceNames) < 1) {
- $this->fail('There are no instances to remove.');
- return;
- }
- foreach ($this->instanceNames as $instance_name) {
- $instance = field_info_instance('node', $instance_name, $node_type->name);
- $this->assertTrue($instance, "Instance exists, now we'll delete it.");
- field_delete_field($instance_name);
- $instance = field_info_instance('node', $instance_name, $node_type->name);
- $this->assertFalse($instance, 'Instance was deleted.');
- }
- $this->instanceNames = array();
- }
-
-
- * Add and delete all field types through Form API.
- *
- * @access public
- */
- public function formTestGenericFieldNodeAddDeleteForm() {
-
- $account = $this->backdropCreateUser(array(
- 'administer content types',
- 'administer fields',
- ));
- $this->backdropLogin($account);
-
-
- $node_type = $this->backdropCreateContentType();
-
-
- $field_names = $this->formAddAllFields($node_type);
-
-
- foreach ($field_names as $field_name) {
-
- $this->backdropGet('admin/structure/types/manage/' .
- $node_type->name . '/fields/field_' . $field_name . '/delete');
-
- $this->backdropPost(NULL, array(), t('Delete'));
- $this->assertText(t('The field @field has been deleted from the @type content type.',
- array('@field' => $field_name, '@type' => $node_type->name)));
- }
- }
-
-
- * Add all fields using Form API.
- *
- * @param mixed $node_type
- * A content type object. If none is specified,
- * the test fails.
- */
- protected function formAddAllFields($node_type = NULL) {
- if (!$node_type) {
- $this->fail('No content type specified.');
- }
-
- $field_types = $this->getFieldTypes();
-
- $unsafe_field_types = array();
- $field_names = array();
-
- $manage_path = 'admin/structure/types/manage/' . $node_type->name . '/fields';
- foreach ($field_types as $field_type) {
-
- $field_info = field_info_field_types($field_type);
-
- if (isset($field_info['no_ui']) && $field_info['no_ui']) {
- $unsafe_field_types[] = $field_type;
- }
- else {
-
-
- $field_name = backdrop_strtolower($this->randomName(26));
- $field_names[$field_type] = $field_name;
-
- $this->formCreateField($manage_path, $field_type, $field_name,
- $field_info['default_widget'], 1);
- }
- }
-
-
- if (!empty($unsafe_field_types)) {
- debug(
- 'Unable to attach these no_ui fields: ' .
- implode(', ', $unsafe_field_types)
- );
- }
-
-
-
- node_types_rebuild();
- menu_rebuild();
- return $field_names;
- }
-
-
- * Create a field using the content type management form.
- *
- * @param mixed $manage_path
- * Path to our content type management form.
- * @param mixed $field_type
- * The type of field we're adding.
- * @param mixed $field_name
- * The name of the field instance we want.
- * @param mixed $widget_type
- * Which widget would we like?
- * @param mixed $cardinality
- * Cardinality for this field instance.
- */
- protected function formCreateField($manage_path, $field_type, $field_name, $widget_type, $cardinality) {
-
- $this->backdropGet($manage_path);
- $edit = array(
- 'fields[_add_new_field][label]' => $field_name,
- 'fields[_add_new_field][field_name]' => $field_name,
- 'fields[_add_new_field][type]' => $field_type,
- 'fields[_add_new_field][widget_type]' => $widget_type,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
-
-
-
- $this->backdropPost(NULL, array(), t('Save settings'));
-
- debug(
- t('Saved settings for field !field_name with widget !widget_type and cardinality !cardinality',
- array(
- '!field_name' => $field_name,
- '!widget_type' => $widget_type,
- '!cardinality' => $cardinality,
- )
- )
- );
-
- $this->assertText(t('Saved @name configuration.', array('@name' => $field_name)));
- }
-
-
- * Create a node with some field content.
- *
- * @return object
- * Node object for the created node.
- */
- public function createFieldContentForUser(
- $account = NULL,
- $content = 'testable_content',
- $node_type = NULL,
- $instance_name = '',
- $column = NULL
- ) {
- if (!$column) {
- $this->fail('No column name given.');
- return NULL;
- }
- if (!$account) {
- $account = $this->backdropCreateUser(array(
- 'bypass node access',
- 'administer content types',
- 'administer fields',
- ));
- }
- $this->backdropLogin($account);
-
- if (!$node_type) {
- $node_type = $this->codeTestGenericAddAllFields();
- }
-
- if (!$instance_name) {
- $instance_name = reset($this->instanceNames);
- }
- $field = array();
- $field[LANGUAGE_NONE][0][$column] = $content;
-
- $settings = array(
- 'type' => $node_type->name,
- $instance_name => $field,
- );
- $node = $this->backdropCreateNode($settings);
-
- $this->assertTrue($node, 'Node of type ' . $node->type . ' allegedly created.');
-
- $node = node_load($node->nid);
- debug('Loaded node id: ' . $node->nid);
- $this->assertTrue($node->$instance_name, 'Field actually created.');
- $field = $node->$instance_name;
- $this->assertTrue($field[LANGUAGE_NONE][0][$column] == $content,
- 'Content was stored properly on the field.');
- return $node;
- }
-
- }
-
- class FieldTestPermissionsExample extends GenericFieldTest {
-
-
- * {@inheritdoc}
- */
- public function setUp() {
- parent::setUp(array('field_permission_example'));
- }
-
-
- * {@inheritdoc}
- */
- protected function getFieldTypes() {
- return array('field_permission_example_fieldnote');
- }
-
-
- * {@inheritdoc}
- */
- protected function getModule() {
- return 'field_permission_example';
- }
-
-
- * Override createFieldContentForUser().
- *
- * We override so we can make sure $column is set to 'notes'.
- */
- public function createFieldContentForUser(
- $account = NULL,
- $content = 'fieldnote_testable_content',
- $node_type = NULL,
- $instance_name = '',
- $column = 'notes'
- ) {
- return parent::createFieldContentForUser($account, $content, $node_type, $instance_name, $column);
- }
-
-
-
- * Test of hook_field_info() and other implementation requirements.
- *
- * @see GenericFieldTest::runTestGenericFieldInfo()
- */
- public function testFieldnoteInfo() {
- $this->runTestGenericFieldInfo();
- }
-
-
- * Add and remove the field through Form API.
- */
- public function testAddRemoveFieldnoteForm() {
- $this->formTestGenericFieldNodeAddDeleteForm();
- }
-
-
- * Add and remove the field through code.
- */
- public function testAddRemoveFieldnoteCode() {
- $node_type = $this->codeTestGenericAddAllFields();
- $this->codeTestGenericRemoveAllFields($node_type);
- }
-
-
- * Test view permissions.
- */
- public function testFieldnoteViewPerms() {
-
-
- $view_own_content = $this->randomName(23);
- $view_any_content = $this->randomName(23);
- $view_own_node = $this->createFieldContentForUser(NULL, $view_own_content);
-
- $node_type = node_type_load($view_own_node->type);
- $view_any_node = $this->createFieldContentForUser(NULL, $view_any_content, $node_type);
-
-
-
- $view_own_account = $this->backdropCreateUser(array(
- 'view own fieldnote',
- ));
- debug("Created user with 'view own fieldnote' permission.");
-
-
- $view_own_node = node_load($view_own_node->nid);
- $view_own_node->uid = $view_own_account->uid;
- node_save($view_own_node);
- $view_own_node = node_load($view_own_node->nid);
- $this->assertTrue($view_own_node->uid == $view_own_account->uid, 'New user assigned to node.');
-
-
-
- $this->backdropLogin($view_own_account);
-
- $this->backdropGet('node/' . $view_own_node->nid);
-
- $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
- $this->assertEqual((string) reset($output_strings), $view_own_content);
- debug("'view own fieldnote' can view own field.");
-
-
-
- $this->backdropGet('node/' . $view_any_node->nid);
-
- $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
- $this->assertNotEqual((string) reset($output_strings), $view_any_content);
- debug("'view own fieldnote' cannot view other field.");
-
-
-
- $view_any_account = $this->backdropCreateUser(array(
- 'view any fieldnote',
- ));
- debug("Created user with 'view any fieldnote' permission.");
- $this->backdropLogin($view_any_account);
-
-
- $this->backdropGet('node/' . $view_any_node->nid);
-
- $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
- $this->assertEqual((string) reset($output_strings), $view_any_content);
- debug("'view any fieldnote' can view other field.");
- }
-
-
- * Test edit permissions.
- *
- * Note that this is mostly identical to testFieldnoteViewPerms() and could
- * probably be refactored.
- */
- public function testFieldnoteEditPerms() {
-
-
- $edit_own_content = $this->randomName(23);
- $edit_any_content = $this->randomName(23);
- $edit_own_node = $this->createFieldContentForUser(NULL, $edit_own_content);
-
- $node_type = node_type_load($edit_own_node->type);
- $edit_any_node = $this->createFieldContentForUser(NULL, $edit_any_content, $node_type);
-
- $edit_own_account = $this->backdropCreateUser(array(
- 'edit own ' . $node_type->name . ' content',
- 'edit own fieldnote',
- ));
- debug("Created user with 'edit own fieldnote' permission.");
-
-
- $edit_own_node = node_load($edit_own_node->nid);
- $edit_own_node->uid = $edit_own_account->uid;
- node_save($edit_own_node);
- $edit_own_node = node_load($edit_own_node->nid);
- $this->assertTrue($edit_own_node->uid == $edit_own_account->uid, 'New edit test user assigned to node.');
-
-
-
- $this->backdropLogin($edit_own_account);
-
- $this->backdropGet('node/' . $edit_own_node->nid . '/edit');
- $this->assertText($edit_own_content, "'edit own fieldnote' can edit own fieldnote.");
-
-
-
- $this->backdropGet('node/' . $edit_any_node->nid . '/edit');
- $this->assertNoText($edit_any_content, "'edit own fieldnote' can not edit any fieldnote.");
-
-
-
-
-
- $edit_any_account = $this->backdropCreateUser(array(
- 'edit any ' . $node_type->name . ' content',
- 'edit any fieldnote',
- ));
- debug("Created user with 'edit any fieldnote' permission.");
- $this->backdropLogin($edit_any_account);
-
-
- $this->backdropGet('node/' . $edit_any_node->nid . '/edit');
- $this->assertText($edit_any_content, "'edit any fieldnote' can edit any fieldnote.");
- }
-
- }