Line # | Frequency | Source Line |
1 | | <?php
|
2 | | // $Id: blogapi.test,v 1.9 2008/04/01 23:33:54 boombatower Exp $
|
3 | |
|
4 | | class BlogAPITestCase extends DrupalTestCase {
|
5 | | function getInfo() {
|
6 | | return array(
|
7 | 1 | 'name' => t('BlogAPI functionality'),
|
8 | | 'description' => t('Create, edit, and delete post; upload file; and set/get categories.'),
|
9 | | 'group' => t('Blog API Tests'),
|
10 | | );
|
11 | | }
|
12 | |
|
13 | | function setUp() {
|
14 | | parent::setUp();
|
15 | |
|
16 | | $this->drupalModuleEnable('blog');
|
17 | | $this->drupalModuleEnable('blogapi');
|
18 | | $this->drupalModuleEnable('taxonomy');
|
19 | | }
|
20 | |
|
21 | | function testBlogAPI() {
|
22 | | // Create admin user and taxononmy for later use.
|
23 | | $admin_user = $this->drupalCreateUser(array('administer taxonomy'));
|
24 | | $this->drupalLogin($admin_user);
|
25 | |
|
26 | | $vid = $this->addVocabulary('simpletest_vocab');
|
27 | |
|
28 | | $term = $this->addTerm($vid, 'simpletest_term1');
|
29 | |
|
30 | | $this->drupalGet('logout');
|
31 | |
|
32 | | // Create user.
|
33 | | $web_user = $this->drupalCreateUser(array('create blog entries', 'delete own blog entries', 'edit own blog entries', 'administer content with blog api'));
|
34 | | $this->drupalLogin($web_user);
|
35 | |
|
36 | | // Init common variables.
|
37 | | $local = url('xmlrpc.php', array('absolute' => TRUE));
|
38 | | $appid = 'simpletest';
|
39 | |
|
40 | | // Get user's blog.
|
41 | | $result = xmlrpc($local, 'blogger.getUsersBlogs', $appid, $web_user->name, $web_user->pass_raw);
|
42 | | $this->assertTrue($result, 'Request for user\'s blogs returned correctly.');
|
43 | |
|
44 | | if ($result !== FALSE) {
|
45 | | $this->assertTrue(array_key_exists('url', $result[0]), 'Blog found.');
|
46 | |
|
47 | | if (array_key_exists('url', $result[0])) {
|
48 | | $blog_id = substr($result[0]['url'], strrpos($result[0]['url'], '/') + 1);
|
49 | | }
|
50 | | }
|
51 | |
|
52 | | // Create post.
|
53 | | $content = $this->randomName(10);
|
54 | | $result = xmlrpc($local, 'blogger.newPost', $appid, $blog_id, $web_user->name, $web_user->pass_raw, $content, TRUE);
|
55 | | $this->assertTrue($result, 'Post created.');
|
56 | |
|
57 | | $nid = $result;
|
58 | |
|
59 | | // Check recent posts.
|
60 | | $result = xmlrpc($local, 'blogger.getRecentPosts', $appid, $blog_id, $web_user->name, $web_user->pass_raw, 5);
|
61 | | $this->assertTrue($result, 'Recent post list retreived.');
|
62 | |
|
63 | | if ($result !== FALSE && array_key_exists('title', $result[0])) {
|
64 | | $this->assertEqual($content, $result[0]['title'], 'Post found.');
|
65 | | }
|
66 | | else
|
67 | | $this->assertTrue(false, 'Post found.');
|
68 | |
|
69 | | // Edit post.
|
70 | | $content_new = $this->randomName(10);
|
71 | | $result = xmlrpc($local, 'blogger.editPost', $appid, $nid, $web_user->name, $web_user->pass_raw, $content_new, TRUE);
|
72 | | $this->assertTrue($result, 'Post successfully modified.');
|
73 | |
|
74 | | // Upload file.
|
75 | | $file_contents = 'This is a test file that will be transfered via BlogAPI!';
|
76 | | $file = array();
|
77 | | $file['name'] = $this->randomName() .'.txt';
|
78 | | $file['type'] = 'text';
|
79 | | $file['bits'] = xmlrpc_base64($file_contents);
|
80 | | $result = xmlrpc($local, 'metaWeblog.newMediaObject', $blog_id, $web_user->name, $web_user->pass_raw, $file);
|
81 | | $this->assertTrue($result, 'File successfully uploaded.');
|
82 | |
|
83 | | $url = (array_key_exists('url', $result) ? $result['url'] : '');
|
84 | |
|
85 | | // Check uploaded file.
|
86 | | $this->drupalGet($url);
|
87 | | $this->assertEqual($this->drupalGetContent(), $file_contents, 'Uploaded contents verified.');
|
88 | |
|
89 | | // Set post categories.
|
90 | | $categories = array(array('categoryId' => $term));
|
91 | | $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
|
92 | | $this->assertTrue($result, 'Post categories set.');
|
93 | |
|
94 | | // Get post categories.
|
95 | | $result = xmlrpc($local, 'mt.getPostCategories', $nid, $web_user->name, $web_user->pass_raw);
|
96 | | $this->assertTrue($result, 'Category list successfully retreived.');
|
97 | |
|
98 | | if ($result !== FALSE && array_key_exists('categoryId', $result[0])) {
|
99 | | $this->assertEqual($term, $result[0]['categoryId'], 'Category list verified.');
|
100 | | }
|
101 | |
|
102 | | // Delete post.
|
103 | | $result = xmlrpc($local, 'blogger.deletePost', $appid, $nid, $web_user->name, $web_user->pass_raw, TRUE);
|
104 | | $this->assertTrue($result, 'Post successfully deleted.');
|
105 | |
|
106 | | $this->drupalGet('logout');
|
107 | |
|
108 | | // Remove taxonmy vocab.
|
109 | | $this->drupalLogin($admin_user);
|
110 | |
|
111 | | $this->drupalPost('admin/content/taxonomy/edit/vocabulary/'. $vid, array(), t('Delete'));
|
112 | | $this->drupalPost(NULL, array(), t('Delete'));
|
113 | | $this->assertRaw(t('Deleted vocabulary %vocab.', array('%vocab' => 'simpletest_vocab')), 'Removed vocabulary.');
|
114 | | }
|
115 | |
|
116 | | /**
|
117 | | * Add taxonomy vocabulary.
|
118 | | *
|
119 | | * @param string $vocab Vocabulary name.
|
120 | | */
|
121 | | function addVocabulary($vocab) {
|
122 | | $edit = array();
|
123 | | $edit['name'] = $vocab;
|
124 | | $edit['nodes[blog]'] = TRUE;
|
125 | | $this->drupalPost('admin/content/taxonomy/add/vocabulary', $edit, t('Save'));
|
126 | | $this->assertRaw(t('Created new vocabulary %vocab.', array('%vocab' => $edit['name'])), 'Taxonomy vocabulary added.');
|
127 | |
|
128 | | $vocab_arr = taxonomy_get_vocabularies();
|
129 | | $vid = NULL;
|
130 | | foreach ($vocab_arr as $vocab_item) {
|
131 | | if ($vocab_item->name == $vocab) {
|
132 | | $vid = $vocab_item->vid;
|
133 | | break;
|
134 | | }
|
135 | | }
|
136 | |
|
137 | | $this->assertNotNull($vid, 'Vocabulary found in database.');
|
138 | | return $vid;
|
139 | | }
|
140 | |
|
141 | | /**
|
142 | | * Add a taxonomy term to vocabulary.
|
143 | | *
|
144 | | * @param integer $vid Vocabulary id.
|
145 | | * @param string $term Term name.
|
146 | | */
|
147 | | function addTerm($vid, $term) {
|
148 | | $edit = array();
|
149 | | $edit['name'] = $term;
|
150 | | $this->drupalPost('admin/content/taxonomy/'. $vid .'/add/term', $edit, t('Save'));
|
151 | | $this->assertRaw(t('Created new term %term.', array('%term' => $edit['name'])), 'Taxonomy term added.');
|
152 | |
|
153 | | $tree = taxonomy_get_tree($vid);
|
154 | | $tid = NULL;
|
155 | | foreach ($tree as $tree_term) {
|
156 | | if ($tree_term->name == $term) {
|
157 | | $tid = $tree_term->tid;
|
158 | | break;
|
159 | | }
|
160 | | }
|
161 | |
|
162 | | $this->assertNotNull($tid, 'Term found in database.');
|
163 | | return $tid;
|
164 | | }
|
165 | | }
|