You are here

function BlogAPIModuleTestCase::test_blog_API in SimpleTest 6

File

tests/blogapi_module.test, line 20

Class

BlogAPIModuleTestCase

Code

function test_blog_API() {

  // Create admin user and taxononmy for later use.
  $admin_user = $this
    ->drupalCreateUserRolePerm(array(
    'administer taxonomy',
  ));
  $this
    ->drupalLoginUser($admin_user);
  $vid = $this
    ->add_vocabulary('simpletest_vocab');
  $term = $this
    ->add_term($vid, 'simpletest_term1');
  $this
    ->drupalGet('logout');

  // Create user.
  $web_user = $this
    ->drupalCreateUserRolePerm(array(
    'create blog entries',
    'delete own blog entries',
    'edit own blog entries',
    'administer content with blog api',
  ));
  $this
    ->drupalLoginUser($web_user);

  // Init common variables.
  $local = url('xmlrpc.php', array(
    'absolute' => TRUE,
  ));
  $appid = 'simpletest';

  // Get user's blog.
  $result = xmlrpc($local, 'blogger.getUsersBlogs', $appid, $web_user->name, $web_user->pass_raw);
  $this
    ->assertTrue($result, 'Request for user\'s blogs returned correctly.');
  if ($result !== FALSE) {
    $this
      ->assertTrue(array_key_exists('url', $result[0]), 'Blog found.');
    if (array_key_exists('url', $result[0])) {
      $blog_id = substr($result[0]['url'], strrpos($result[0]['url'], '/') + 1);
    }
  }

  // Create post.
  $content = $this
    ->randomName(10);
  $result = xmlrpc($local, 'blogger.newPost', $appid, $blog_id, $web_user->name, $web_user->pass_raw, $content, TRUE);
  $this
    ->assertTrue($result, 'Post created.');
  $nid = $result;

  // Check recent posts.
  $result = xmlrpc($local, 'blogger.getRecentPosts', $appid, $blog_id, $web_user->name, $web_user->pass_raw, 5);
  $this
    ->assertTrue($result, 'Recent post list retreived.');
  if ($result !== FALSE && array_key_exists('title', $result[0])) {
    $this
      ->assertEqual($content, $result[0]['title'], 'Post found.');
  }
  else {
    $this
      ->assertTrue(false, 'Post found.');
  }

  // Edit post.
  $content_new = $this
    ->randomName(10);
  $result = xmlrpc($local, 'blogger.editPost', $appid, $nid, $web_user->name, $web_user->pass_raw, $content_new, TRUE);
  $this
    ->assertTrue($result, 'Post successfully modified.');

  // Upload file.
  $file_contents = 'This is a test file that will be transfered via BlogAPI!';
  $file = array();
  $file['name'] = $this
    ->randomName() . '.txt';
  $file['type'] = 'text';
  $file['bits'] = xmlrpc_base64($file_contents);
  $result = xmlrpc($local, 'metaWeblog.newMediaObject', $blog_id, $web_user->name, $web_user->pass_raw, $file);
  $this
    ->assertTrue($result, 'File successfully uploaded.');
  $url = array_key_exists('url', $result) ? $result['url'] : '';

  // Check uploaded file.
  $this
    ->drupalGet($url);
  $this
    ->assertEqual($this
    ->drupalGetContent(), $file_contents, 'Uploaded contents verified.');

  // Set post categories.
  $categories = array(
    array(
      'categoryId' => $term,
    ),
  );
  $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
  $this
    ->assertTrue($result, 'Post categories set.');

  // Get post categories.
  $result = xmlrpc($local, 'mt.getPostCategories', $nid, $web_user->name, $web_user->pass_raw);
  $this
    ->assertTrue($result, 'Category list successfully retreived.');
  if ($result !== FALSE && array_key_exists('categoryId', $result[0])) {
    $this
      ->assertEqual($term, $result[0]['categoryId'], 'Category list verified.');
  }

  // Delete post.
  $result = xmlrpc($local, 'blogger.deletePost', $appid, $nid, $web_user->name, $web_user->pass_raw, TRUE);
  $this
    ->assertTrue($result, 'Post successfully deleted.');
  $this
    ->drupalGet('logout');

  // Remove taxonmy vocab.
  $this
    ->drupalLoginUser($admin_user);
  $this
    ->drupalPost('admin/content/taxonomy/edit/vocabulary/' . $vid, array(), 'Delete');
  $this
    ->drupalPost(NULL, array(), 'Delete');
  $this
    ->assertWantedRaw(t('Deleted vocabulary %vocab.', array(
    '%vocab' => 'simpletest_vocab',
  )), 'Removed vocabulary.');
}