You are here

blogapi_metaweblog.test in Blog API 7.2

Endpoint tests for BlogAPI Metaweblog

File

modules/blogapi_metaweblog/blogapi_metaweblog.test
View source
<?php

/**
 * @file
 * Endpoint tests for BlogAPI Metaweblog
 */
class BlogapiMetaweblogEndpointTestCase extends BlogapiTestCase {

  /**
   * Test info.
   */
  public static function getInfo() {
    return array(
      'name' => t('Metaweblog Endpoint Tests'),
      'description' => t('Run tests for Metaweblog endpoints'),
      'group' => t('BlogAPI'),
    );
  }

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('blogapi_metaweblog');
  }

  /**
   * Test metaWeblog.newPost().
   */
  public function testNewPost() {

    // Prepare data for nodes
    $node_title = $this
      ->randomString();
    $node_body = $this
      ->randomString();
    $content = array(
      'title' => $node_title,
      'description' => $node_body,
    );
    $missed_type = '_arcticle';

    // Try to create the node. This should fail because the user does not have
    // permission to create this content type.
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.newPost' => array(
        $missed_type,
        $this->privilegedUser->name,
        $this->privilegedUser->pass_raw,
        $content,
        TRUE,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), 'You do not have permission to create this type of post.', 'Node access is being respected.');

    // Try to create the node. This should fail because the user does not have
    // permission to create this content type.
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.newPost' => array(
        $missed_type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        $content,
        TRUE,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), format_string('BlogAPI is not configured to support the @type content type.', array(
      '@type' => $missed_type,
    )), $missed_type . ' type is missed.');

    // Create a content type.
    $type = $this
      ->drupalCreateContentType();

    // Test with existing content type but not allowed for BlogAPI
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.newPost' => array(
        $type->type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        $content,
        TRUE,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), format_string('BlogAPI is not configured to support the @type content type.', array(
      '@type' => $type->type,
    )), $type->type . ' is not allowed for BlogAPI yet.');

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $type->type,
    ));

    // Create the node. This should work because the user has administer nodes.
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.newPost' => array(
        $type->type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        $content,
        TRUE,
      ),
    ));

    // Load the node and validate the data.
    $node = node_load($nid);
    $this
      ->assertEqual($node->title, $node_title, 'New node title is set correctly.');
    $this
      ->assertEqual($node->body[LANGUAGE_NONE][0]['value'], $node_body, 'New node body is set correctly.');
    $this
      ->assertEqual($node->status, 1, 'New node is published');
  }

  /**
   * Test metaWeblog.editPost().
   */
  public function testEditPost() {

    // Prepare data for nodes
    $node_title = $this
      ->randomString();
    $node_body = $this
      ->randomString();
    $content = array(
      'title' => $node_title,
      'description' => $node_body,
    );
    $allowed_type = $this
      ->drupalCreateContentType();
    $disallowed_type = $this
      ->drupalCreateContentType();

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $allowed_type->type,
    ));
    $wrong_node = $this
      ->drupalCreateNode(array(
      'type' => $disallowed_type->type,
    ));
    $correct_node = $this
      ->drupalCreateNode(array(
      'type' => $allowed_type->type,
      'title' => $node_title,
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $node_body,
          ),
        ),
      ),
    ));

    // Test edit unexisting node
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.editPost' => array(
        0,
        $this->privilegedUser->name,
        $this->privilegedUser->pass_raw,
        $content,
        TRUE,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), format_string('Node @nid not found', array(
      '@nid' => 0,
    )), 'Not found error returned');

    // Try to update node with unprevileged user
    $nid = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.editPost' => array(
        $correct_node->nid,
        $this->privilegedUser->name,
        $this->privilegedUser->pass_raw,
        $content,
        TRUE,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), 'You do not have permission to update this post.', 'User must have node edit permissions');
    $node_title .= ' modified title';
    $node_body .= ' modified body';
    $result = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.editPost' => array(
        $correct_node->nid,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        array(
          'title' => $node_title,
          'description' => $node_body,
        ),
        TRUE,
      ),
    ));
    $new_node = node_load($correct_node->nid, NULL, TRUE);
    $this
      ->assertTrue($result && $new_node->title != $correct_node->title, 'Node was updated');
    $this
      ->assertTrue($new_node->title == $node_title, 'Title was updated');
    $this
      ->assertTrue(!empty($new_node->body[LANGUAGE_NONE][0]['value']) && $new_node->body[LANGUAGE_NONE][0]['value'] == $node_body, 'Body was updated');
  }

  /**
   * Test metaWeblog.getPost().
   */
  public function testGetPost() {
    $type = $this
      ->drupalCreateContentType();

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $type->type,
    ));
    $node = $this
      ->drupalCreateNode(array(
      'type' => $type->type,
      'uid' => $this->privilegedUser2->uid,
    ));

    // Test user without node view permission.
    $result = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.getPost' => array(
        $node->nid,
        $this->privilegedUser->name,
        $this->privilegedUser->pass_raw,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), format_string('You are not authorized to view post @postid', array(
      '@postid' => $node->nid,
    )), 'User must node view permissions');

    // Normal test
    $result = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.getPost' => array(
        $node->nid,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
      ),
    ));
    $this
      ->assertTrue($result['userid'] == $this->privilegedUser2->name, 'Author name is correct');
    $this
      ->assertTrue($result['dateCreated'] == xmlrpc_date($node->created), 'Created date is correct');
    $this
      ->assertTrue($result['title'] == $node->title, 'Post title is correct');
    $this
      ->assertTrue($result['postid'] == $node->nid, 'Post ID is correct');
    $node_body = !empty($node->body[LANGUAGE_NONE][0]['value']) ? $node->body[LANGUAGE_NONE][0]['value'] : '';
    $this
      ->assertTrue($result['content'] == '<title>' . $node->title . '</title>' . $node_body, 'Post content is correct');
    $this
      ->assertTrue($result['link'] == url('node/' . $node->nid, array(
      'absolute' => TRUE,
    )), 'Post link is correct');
  }

  /**
   * Test metaWeblog.newMediaObject().
   * @TODO finish this test
   */
  public function testNewMediaObject() {
    $type = $this
      ->drupalCreateContentType();
    $this->testfiles = $this
      ->drupalGetTestFiles('image');
    $this->testfile = current($this->testfiles);

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $type->type,
    ));
    $node = $this
      ->drupalCreateNode(array(
      'type' => $type->type,
      'uid' => $this->privilegedUser2->uid,
    ));
    $result = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.newMediaObject' => array(
        $type->type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        array(
          'name' => $this->testfile->filename,
          'type' => mime_content_type(file_create_url($this->testfile->uri)),
          'bits' => base64_encode(file_get_contents(file_create_url($this->testfile->uri))),
        ),
      ),
    ));
    $this
      ->assertTrue(is_file($result['url']), 'New file saved to disk.', 'FileResource: Create');
  }

  /**
   * Test metaWeblog.getCategories().
   */
  public function testGetCategories() {
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer content types',
      'administer taxonomy',
      'bypass node access',
      'administer services',
    ));
    $this
      ->drupalLogin($this->admin_user);
    $this->vocabulary = $this
      ->createVocabulary();
    $type = $this
      ->drupalCreateContentType();
    $field = array(
      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $this->vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);
    $this->instance = array(
      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
      'bundle' => $type->type,
      'entity_type' => 'node',
      'widget' => array(
        'type' => 'options_select',
      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
        ),
      ),
    );
    field_create_instance($this->instance);
    $terms = array();
    for ($i = 0; $i < 5; $i++) {
      $terms[] = $this
        ->createTerm($this->vocabulary);
    }
    $testNode = array(
      'type' => $type->type,
      $field['field_name'] => array(
        LANGUAGE_NONE => array(
          array(
            'tid' => $terms[0]->tid,
          ),
        ),
      ),
      'uid' => $this->privilegedUser2->uid,
    );

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $type->type,
    ));
    $node = $this
      ->drupalCreateNode($testNode);
    $results = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.getCategories' => array(
        $type->type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
      ),
    ));
    $this
      ->assertTrue(is_array($results) && count($results) == count($terms), 'Service returned correct terms number');
    $success = TRUE;
    foreach ($results as $category) {
      $term = taxonomy_term_load($category['categoryId']);
      if (empty($term) || $term->name != $category['categoryName']) {
        $success = FALSE;
      }
    }
    $this
      ->assertTrue($success, 'All terms were retrieved properly');
  }

  /**
   * test metaWeblog.getRecentPosts().
   */
  public function testGetRecentPosts() {
    $type = $this
      ->drupalCreateContentType();
    $number_of_posts = 9;
    $nodes = array();
    for ($i = 0; $i < 11; $i++) {
      $node = $this
        ->drupalCreateNode(array(
        'type' => $type->type,
        'uid' => $this->privilegedUser2->uid,
      ));
      $nodes[$node->nid] = $node;
    }

    // Test user without node view permission.
    $result = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.getRecentPosts' => array(
        $type->type,
        $this->privilegedUser->name,
        $this->privilegedUser->pass_raw,
        $number_of_posts,
      ),
    ));
    $this
      ->assertEqual(xmlrpc_error_msg(), format_string('BlogAPI is not configured to support the @type content type.', array(
      '@type' => $type->type,
    )), $type->type . ' is not allowed for BlogAPI yet.');

    // Allow to user our content type with BlogAPI
    variable_set('blogapi_node_types', array(
      $type->type,
    ));
    $results = xmlrpc($this->xmlrpcUrl, array(
      'metaWeblog.getRecentPosts' => array(
        $type->type,
        $this->privilegedUser2->name,
        $this->privilegedUser2->pass_raw,
        $number_of_posts,
      ),
    ));
    $this
      ->assertTrue(is_array($results) && count($results) == $number_of_posts, 'Service returned correct posts number');
    $success = TRUE;
    foreach ($results as $post) {
      $node = node_load($post['postid']);
      if (empty($node) || $node->title != $nodes[$post['postid']]->title || !empty($node->body[LANGUAGE_NONE][0]['value']) && $node->body[LANGUAGE_NONE][0]['value'] != $nodes[$post['postid']]->body[LANGUAGE_NONE][0]['value']) {
        $success = FALSE;
      }
    }
    $this
      ->assertTrue($success, 'All nodes were retrieved properly');
  }

}

Classes

Namesort descending Description
BlogapiMetaweblogEndpointTestCase @file Endpoint tests for BlogAPI Metaweblog