From fd7017f5303035ca20ee441e67aa6aedb794ae31 Mon Sep 17 00:00:00 2001 From: Christian Mehlmauer Date: Tue, 28 May 2013 19:45:20 +0200 Subject: [PATCH] readded "junk removal" from usernames before output --- lib/common/collections/wp_users/output.rb | 18 +++++ lib/common/common_helper.rb | 25 +++++++ .../collections/wp_users/output_spec.rb | 72 +++++++++++++++++++ spec/lib/common/common_helper_spec.rb | 59 +++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 spec/lib/common/collections/wp_users/output_spec.rb create mode 100644 spec/lib/common/common_helper_spec.rb diff --git a/lib/common/collections/wp_users/output.rb b/lib/common/collections/wp_users/output.rb index c920144a..7d04733e 100644 --- a/lib/common/collections/wp_users/output.rb +++ b/lib/common/collections/wp_users/output.rb @@ -12,6 +12,8 @@ class WpUsers < WpItems headings = ['Id', 'Login', 'Name'] headings << 'Password' if options[:show_password] + remove_junk_from_display_names + self.each do |wp_user| row = [wp_user.id, wp_user.login, wp_user.display_name] row << wp_user.password if options[:show_password] @@ -25,5 +27,21 @@ class WpUsers < WpItems puts table end + def remove_junk_from_display_names + display_names = [] + self.each do |u| + display_name = u.display_name + unless display_name == 'empty' + display_names << display_name + end + end + junk = get_equal_string_end(display_names) + unless junk.nil? or junk.empty? + self.each do |u| + u.display_name = u.display_name.sub(/#{Regexp.escape(junk)}$/, '') + end + end + end + end end diff --git a/lib/common/common_helper.rb b/lib/common/common_helper.rb index 71f54a36..42c7cf61 100644 --- a/lib/common/common_helper.rb +++ b/lib/common/common_helper.rb @@ -112,3 +112,28 @@ def redefine_constant(constant, value) Object.send(:remove_const, constant) Object.const_set(constant, value) end + +# Gets the string all elements in stringarray ends with +def get_equal_string_end(stringarray = ['']) + already_found = '' + looping = true + counter = -1 + if stringarray.kind_of? Array and stringarray.length > 1 + base = stringarray[0] + while looping + character = base[counter, 1] + stringarray.each do |s| + if s[counter, 1] != character + looping = false + break + end + end + if looping == false or (counter * -1) > base.length + break + end + already_found = "#{character if character}#{already_found}" + counter -= 1 + end + end + already_found +end \ No newline at end of file diff --git a/spec/lib/common/collections/wp_users/output_spec.rb b/spec/lib/common/collections/wp_users/output_spec.rb new file mode 100644 index 00000000..2e9aea23 --- /dev/null +++ b/spec/lib/common/collections/wp_users/output_spec.rb @@ -0,0 +1,72 @@ +# encoding: UTF-8 + +require 'spec_helper' + +describe 'WpUsers::Output' do + subject(:wp_users) { WpUsers.new(0) } + let(:wp_content_dir) { 'wp-content' } + let(:wp_plugins_dir) { wp_content_dir + '/plugins' } + let(:wp_target) { WpTarget.new(url, wp_content_dir: wp_content_dir, wp_plugins_dir: wp_plugins_dir) } + let(:url) { 'http://example.com/' } + let(:uri) { URI.parse(url) } + + describe '#remove_junk_from_display_names' do + it 'does not throw an exception' do + expect { subject.remove_junk_from_display_names }.to_not raise_error + end + end + + describe '#remove_junk_from_display_names' do + before :each do + @input = WpUsers.new(0) + end + + after :each do + subject.push(@input) + subject.flatten! + subject.remove_junk_from_display_names + subject.should === @expected + end + + it 'should return an empty array' do + @expected = @input + end + + it 'should return input object' do + @input.push(WpUser.new(nil)) + @expected = @input + end + + it 'should return input object' do + @input.push(WpUser.new('')) + @expected = @input + end + + it 'should remove asdf' do + @input.push(WpUser.new('', login: '', id: 1, display_name: 'lkjh asdf')) + @input.push(WpUser.new('', login: '', id: 2, display_name: 'ijrjd asdf')) + @expected = WpUsers.new(0) + @expected.push(WpUser.new('', login: '', id: 1, display_name: 'lkjh')) + @expected.push(WpUser.new('', login: '', id: 2, display_name: 'ijrjd')) + end + + it 'should return unmodified input object' do + @input.push(WpUser.new('', login: '', id: 1, display_name: 'lkjh asdfa')) + @input.push(WpUser.new('', login: '', id: 2, display_name: 'ijrjd asdf')) + @expected = @input + end + + it 'should return input object' do + @input.push(WpUser.new('', login: '', id: 1, display_name: 'lkjh asdf')) + @expected = @input + end + + it 'should return an empty display_name' do + @input.push(WpUser.new('', login: '', id: 1, display_name: 'lkhj asdf')) + @input.push(WpUser.new('', login: '', id: 2, display_name: 'lkhj asdf')) + @expected = WpUsers.new(0) + @expected.push(WpUser.new('', login: '', id: 1, display_name: '')) + @expected.push(WpUser.new('', login: '', id: 2, display_name: '')) + end + end +end diff --git a/spec/lib/common/common_helper_spec.rb b/spec/lib/common/common_helper_spec.rb new file mode 100644 index 00000000..1069df16 --- /dev/null +++ b/spec/lib/common/common_helper_spec.rb @@ -0,0 +1,59 @@ +# encoding: UTF-8 + +require 'spec_helper' + +describe 'common_helper' do + describe '#get_equal_string' do + after :each do + output = get_equal_string_end(@input) + + output.should == @expected + end + + it 'returns an empty string' do + @input = %w() + @expected = '' + end + + it 'returns an empty string' do + @input = [] + @expected = '' + end + + it 'returns asdf' do + @input = ['kjh asdf', 'oijr asdf'] + @expected = ' asdf' + end + + it 'returns « BlogName' do + @input = ['user1 « BlogName', + 'user2 « BlogName', + 'user3 « BlogName', + 'user4 « BlogName'] + @expected = ' « BlogName' + end + + it 'returns an empty string' do + @input = %w{user1 user2 user3 user4} + @expected = '' + end + + it 'returns an empty string' do + @input = ['user1 « BlogName', + 'user2 « BlogName', + 'user3 « BlogName', + 'user4 « BlogNamea'] + @expected = '' + end + + it 'returns an empty string' do + @input = %w{ user1 } + @expected = '' + end + + it 'returns | test' do + @input = ['admin | test', 'test | test'] + @expected = ' | test' + end + end +end \ No newline at end of file