From a9b25edafec2e9cc3f3e2e4cc8ae50a9973feafb Mon Sep 17 00:00:00 2001 From: erwanlr Date: Sat, 30 Mar 2013 19:08:47 +0100 Subject: [PATCH] WpUser additional specs --- lib/common/hacks.rb | 1 + lib/common/models/wp_user.rb | 5 ++++ lib/common/models/wp_user/existable.rb | 16 +++++++++---- spec/lib/common/models/wp_user_spec.rb | 28 +++++++++++++++++++++++ spec/shared_examples/wp_user_existable.rb | 9 +++++++- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/lib/common/hacks.rb b/lib/common/hacks.rb index 9886be77..c4717d90 100644 --- a/lib/common/hacks.rb +++ b/lib/common/hacks.rb @@ -43,6 +43,7 @@ module Typhoeus body_hash != error_404_hash && body_hash != homepage_hash end + end end diff --git a/lib/common/models/wp_user.rb b/lib/common/models/wp_user.rb index 6e549622..46c13dd6 100755 --- a/lib/common/models/wp_user.rb +++ b/lib/common/models/wp_user.rb @@ -7,6 +7,7 @@ class WpUser < WpItem attr_accessor :id, :login, :display_name, :password + # @return [ Array ] def allowed_options; [:id, :login, :display_name, :password] end # @return [ URI ] The uri to the auhor page @@ -32,11 +33,15 @@ class WpUser < WpItem end # @param [ WpUser ] other + # + # @return [ Boolean ] def ==(other) self === other end # @param [ WpUser ] other + # + # @return [ Boolean ] def ===(other) id === other.id && login === other.login end diff --git a/lib/common/models/wp_user/existable.rb b/lib/common/models/wp_user/existable.rb index 0412db17..924f3981 100755 --- a/lib/common/models/wp_user/existable.rb +++ b/lib/common/models/wp_user/existable.rb @@ -22,13 +22,13 @@ class WpUser < WpItem if response.code == 301 # login in location? location = response.headers_hash['Location'] - @login = WpUser::Existable.login_from_author_pattern(location) - @display_name = WpUser::Existable.display_name_from_body( + @login = Existable.login_from_author_pattern(location) + @display_name = Existable.display_name_from_body( Browser.instance.get(location).body ) elsif response.code == 200 # login in body? - @login = WpUser::Existable.login_from_body(response.body) - @display_name = WpUser::Existable.display_name_from_body(response.body) + @login = Existable.login_from_body(response.body) + @display_name = Existable.display_name_from_body(response.body) end end private :load_from_response @@ -55,16 +55,22 @@ class WpUser < WpItem login end + # @note Some bodies are encoded in ASCII-8BIT, and Nokogiri doesn't support it + # So it's forced to UTF-8 when this encoding is detected + # # @param [ String ] body # # @return [ String ] The display_name def self.display_name_from_body(body) if title_tag = body[%r{([^<]+)}i, 1] + title_tag.force_encoding('UTF-8') if title_tag.encoding == Encoding::ASCII_8BIT title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s # & are not decoded with Nokogiri title_tag.sub!('&', '&') - return title_tag[%r{([^|«]+) }, 1] + name = title_tag[%r{([^|«]+) }, 1] + + return name.strip if name end end diff --git a/spec/lib/common/models/wp_user_spec.rb b/spec/lib/common/models/wp_user_spec.rb index 6e2545e5..12d10856 100644 --- a/spec/lib/common/models/wp_user_spec.rb +++ b/spec/lib/common/models/wp_user_spec.rb @@ -33,6 +33,34 @@ describe WpUser do end end + describe '#to_s' do + after do + subject.id = 1 + subject.to_s.should == @expected + end + + it 'returns @id' do + @expected = '1' + end + + context 'when @login' do + it 'returns @id | @login' do + subject.login = 'admin' + + @expected = '1 | admin' + end + + context 'when @display_name' do + it 'returns @id | @login | @display_name' do + subject.login = 'admin' + subject.display_name = 'real name' + + @expected = '1 | admin | real name' + end + end + end + end + describe '#<=>' do it 'bases the comparaison on the :id' do wp_user.id = 1 diff --git a/spec/shared_examples/wp_user_existable.rb b/spec/shared_examples/wp_user_existable.rb index 59e484f2..f8035cbf 100644 --- a/spec/shared_examples/wp_user_existable.rb +++ b/spec/shared_examples/wp_user_existable.rb @@ -69,6 +69,13 @@ shared_examples 'WpUser::Existable' do end end + context 'when the body is an ASCII-8BIT' do + it 'return the correct display_name' do + @body = 'its me | wordpress'.encode('ASCII-8BIT') + @expected = 'its me' + end + end + context 'when pattern is found' do context 'when unencoded extra chars' do it 'returns the display_name w/o extra chars' do @@ -91,7 +98,7 @@ shared_examples 'WpUser::Existable' do end end - it 'decodes entities in display_name' do + it 'decodes entities' do @body = 'user & nickname | Wordpress-3.5.1' @expected = 'user & nickname' end