Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 3092f98

Browse files
committed
Add Wpxf::WordPress::Comments mixin
1 parent b5281b7 commit 3092f98

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

lib/wpxf/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@ def self.change_stdout_sync(enabled)
7474
require 'wpxf/wordpress/stored_xss'
7575
require 'wpxf/wordpress/shell_upload'
7676
require 'wpxf/wordpress/file_download'
77+
require 'wpxf/wordpress/comments'
7778

7879
require 'wpxf/core/module'

lib/wpxf/wordpress/comments.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
# Provides functionality for gathering and posting comments.
4+
module Wpxf::WordPress::Comments
5+
include Wpxf
6+
7+
def initialize
8+
super
9+
end
10+
11+
# Register the comment posting options.
12+
def wordpress_comments_register_options
13+
register_options([
14+
StringOption.new(
15+
name: 'comment_author',
16+
desc: 'The author name to use when posting a comment',
17+
default: Utility::Text.rand_alpha(5),
18+
required: true
19+
),
20+
StringOption.new(
21+
name: 'comment_content',
22+
desc: 'The static text to use as the comment content',
23+
default: Utility::Text.rand_alpha(20),
24+
required: true
25+
),
26+
StringOption.new(
27+
name: 'comment_email',
28+
desc: 'The e-mail address to use when posting a comment',
29+
default: Utility::Text.rand_email,
30+
required: true
31+
),
32+
StringOption.new(
33+
name: 'comment_website',
34+
desc: 'The website address to use when posting a comment',
35+
required: false
36+
),
37+
IntegerOption.new(
38+
name: 'comment_post_id',
39+
desc: 'The ID of the post to comment on',
40+
required: true
41+
)
42+
])
43+
end
44+
45+
# Post a comment.
46+
# @param post_id [Integer] the post ID to comment on.
47+
# @param content [String] the content of the comment.
48+
# @param author [String] the author's name.
49+
# @param email [String] the author's e-mail address.
50+
# @param website [String] the author's website.
51+
# @return [Integer] the ID of the comment, or -1 if the comment failed to post.
52+
def wordpress_comments_post(post_id, content, author, email, website)
53+
comment_id = -1
54+
55+
scoped_option_change('follow_http_redirection', false) do
56+
res = execute_post_request(
57+
url: wordpress_url_comments_post,
58+
cookie: session_cookie,
59+
body: {
60+
author: author,
61+
comment: content,
62+
email: email,
63+
url: website,
64+
submit: 'Post Comment',
65+
comment_post_ID: post_id,
66+
comment_parent: 0
67+
}
68+
)
69+
70+
if res&.code == 302
71+
id = res.headers['Location'][/#comment-([0-9]+)/i, 1]
72+
comment_id = id.to_i if id
73+
end
74+
end
75+
76+
comment_id
77+
end
78+
end

spec/wordpress/comments_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
5+
describe Wpxf::WordPress::Comments do
6+
let(:body) { '' }
7+
let(:headers) { [] }
8+
let(:code) { 200 }
9+
let(:subject) do
10+
subject = Class.new(Wpxf::Module) do
11+
include Wpxf::Net::HttpClient
12+
include Wpxf::WordPress::Urls
13+
include Wpxf::WordPress::Comments
14+
end.new
15+
16+
subject.set_option_value('host', '127.0.0.1')
17+
subject.set_option_value('target_uri', '/wp/')
18+
subject
19+
end
20+
21+
before :each do
22+
res = Wpxf::Net::HttpResponse.new(nil)
23+
res.body = body
24+
res.code = code
25+
res.headers = headers
26+
allow(subject).to receive(:execute_get_request).and_return(res)
27+
allow(subject).to receive(:execute_post_request).and_return(res)
28+
end
29+
30+
describe '#wordpress_comments_register_options' do
31+
it 'registers a set of comment posting related options' do
32+
subject.wordpress_comments_register_options
33+
34+
options = [
35+
'comment_author',
36+
'comment_content',
37+
'comment_email',
38+
'comment_website',
39+
'comment_post_id'
40+
]
41+
42+
options.each do |o|
43+
expect(subject.get_option(o)).to_not be_nil
44+
end
45+
end
46+
end
47+
48+
describe '#wordpress_comments_post?' do
49+
context 'if the comment ID can be found in the location header' do
50+
let(:code) { 302 }
51+
let(:headers) { { 'Location' => 'http://127.0.0.1/wp/hello-world/#comment-3' } }
52+
53+
it 'returns the comment ID' do
54+
res = subject.wordpress_comments_post(1, 'content', 'author', 'user@localhost', '')
55+
expect(res).to eq 3
56+
end
57+
end
58+
59+
context 'if the comment ID was not in the location header' do
60+
let(:code) { 302 }
61+
let(:headers) { { 'Location' => 'http://127.0.0.1/wp/hello-world/' } }
62+
63+
it 'returns -1' do
64+
res = subject.wordpress_comments_post(1, 'content', 'author', 'user@localhost', '')
65+
expect(res).to eq(-1)
66+
end
67+
end
68+
69+
context 'if the status code is not 302' do
70+
let(:code) { 200 }
71+
it 'returns -1' do
72+
res = subject.wordpress_comments_post(1, 'content', 'author', 'user@localhost', '')
73+
expect(res).to eq(-1)
74+
end
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)