diff --git a/lib/onelogin/ruby-saml/utils.rb b/lib/onelogin/ruby-saml/utils.rb index a135d697e..61ea93635 100644 --- a/lib/onelogin/ruby-saml/utils.rb +++ b/lib/onelogin/ruby-saml/utils.rb @@ -286,7 +286,7 @@ def self.original_uri_match?(destination_url, settings_url) # that there all children other than text nodes can be ignored (e.g. comments). If nil is # passed, nil will be returned. def self.element_text(element) - element.texts.join if element + element.texts.map(&:value).join if element end end end diff --git a/test/utils_test.rb b/test/utils_test.rb index 2bb592e99..18252fa49 100644 --- a/test/utils_test.rb +++ b/test/utils_test.rb @@ -213,5 +213,42 @@ class UtilsTest < Minitest::Test assert !OneLogin::RubySaml::Utils.uri_match?(destination, settings) end end + + describe 'element_text' do + it 'returns the element text' do + element = REXML::Document.new('element text').elements.first + assert_equal 'element text', OneLogin::RubySaml::Utils.element_text(element) + end + + it 'returns all segments of the element text' do + element = REXML::Document.new('element text').elements.first + assert_equal 'element text', OneLogin::RubySaml::Utils.element_text(element) + end + + it 'returns normalized element text' do + element = REXML::Document.new('element & text').elements.first + assert_equal 'element & text', OneLogin::RubySaml::Utils.element_text(element) + end + + it 'returns the CDATA element text' do + element = REXML::Document.new('').elements.first + assert_equal 'element & text', OneLogin::RubySaml::Utils.element_text(element) + end + + it 'returns the element text with newlines and additional whitespace' do + element = REXML::Document.new(" element \n text ").elements.first + assert_equal " element \n text ", OneLogin::RubySaml::Utils.element_text(element) + end + + it 'returns nil when element is nil' do + assert_nil OneLogin::RubySaml::Utils.element_text(nil) + end + + it 'returns empty string when element has no text' do + element = REXML::Document.new('').elements.first + assert_equal '', OneLogin::RubySaml::Utils.element_text(element) + end + + end end end