@@ -7,6 +7,7 @@ class SocialMediaLinksValidatorTest < ActiveSupport::TestCase
77 fields : {
88 "service_field" => "social_media_service_name" ,
99 "url_field" => "url" ,
10+ "title_field" => "title" ,
1011 } ,
1112 } )
1213 end
@@ -78,46 +79,102 @@ class SocialMediaLinksValidatorTestClass
7879 assert_includes block_content . errors [ "social_media_links.0.url" . to_sym ] , "is invalid - use the full URL, including https://"
7980 end
8081
81- test "social media links are invalid if two of the same channel are provided " do
82+ test "social media links are invalid if two channels have the same URL " do
8283 block_content = SocialMediaLinksValidatorTestClass . new
8384 block_content . social_media_links = [
85+ { "social_media_service_name" => "Twitter" , "url" => "http://facebook.com/govuk" } ,
8486 { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govuk" } ,
85- { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govukpage2" } ,
8687 ]
8788 @validator . validate ( block_content )
8889
89- assert_empty block_content . errors [ "social_media_links.0.social_media_service_name " . to_sym ]
90- assert_includes block_content . errors [ "social_media_links.1.social_media_service_name " . to_sym ] , "must be unique"
90+ assert_empty block_content . errors [ "social_media_links.0.url " . to_sym ]
91+ assert_includes block_content . errors [ "social_media_links.1.url " . to_sym ] , "must be unique"
9192 end
9293
93- test "social media links are invalid if two channels have the same URL" do
94+ test "social media links are valid when a channel is chosen and a well-formed URL is provided " do
9495 block_content = SocialMediaLinksValidatorTestClass . new
9596 block_content . social_media_links = [
96- { "social_media_service_name" => "Twitter" , "url" => "http://facebook.com/govuk" } ,
9797 { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govuk" } ,
98+ { "social_media_service_name" => "LinkedIn" , "url" => "https://linkedin.com/company/govuk" } ,
9899 ]
99100 @validator . validate ( block_content )
100101
101- assert_empty block_content . errors [ "social_media_links.0.url" . to_sym ]
102- assert_includes block_content . errors [ "social_media_links.1.url" . to_sym ] , "must be unique"
102+ assert block_content . errors . empty?
103103 end
104104
105- test "social media links are valid when multiple 'Other' channels are provided with different URLs " do
105+ test "social media links are valid when titles are unique " do
106106 block_content = SocialMediaLinksValidatorTestClass . new
107107 block_content . social_media_links = [
108- { "social_media_service_name" => "Other " , "url" => "http://example .com/one " } ,
109- { "social_media_service_name" => "Other " , "url" => "http://example .com/two " } ,
108+ { "social_media_service_name" => "Facebook " , "url" => "http://facebook .com/govuk" , "title" => "GOV.UK on Facebook " } ,
109+ { "social_media_service_name" => "Twitter " , "url" => "http://twitter .com/govuk" , "title" => "GOV.UK on Twitter " } ,
110110 ]
111111 @validator . validate ( block_content )
112112
113113 assert block_content . errors . empty?
114114 end
115115
116- test "social media links are valid when a channel is chosen and a well-formed URL is provided" do
116+ test "social media links are valid when titles are blank" do
117+ block_content = SocialMediaLinksValidatorTestClass . new
118+ block_content . social_media_links = [
119+ { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govuk" , "title" => "" } ,
120+ { "social_media_service_name" => "Twitter" , "url" => "http://twitter.com/govuk" , "title" => "" } ,
121+ ]
122+ @validator . validate ( block_content )
123+
124+ assert block_content . errors . empty?
125+ end
126+
127+ test "social media links are invalid when any two accounts have the same title" do
128+ block_content = SocialMediaLinksValidatorTestClass . new
129+ block_content . social_media_links = [
130+ { "social_media_service_name" => "Facebook" , "title" => "Our updates" , "url" => "http://facebook.com/govuk" } ,
131+ { "social_media_service_name" => "Twitter" , "title" => "Our updates" , "url" => "http://twitter.com/govuk" } ,
132+ ]
133+ @validator . validate ( block_content )
134+
135+ assert_includes block_content . errors [ "social_media_links.1.title" . to_sym ] , "must be unique"
136+ end
137+
138+ test "social media links are invalid when multiple instances of the same channel are provided, with no distinct titles" do
117139 block_content = SocialMediaLinksValidatorTestClass . new
118140 block_content . social_media_links = [
119141 { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govuk" } ,
120- { "social_media_service_name" => "LinkedIn" , "url" => "https://linkedin.com/company/govuk" } ,
142+ { "social_media_service_name" => "Facebook" , "url" => "http://facebook.com/govukpage2" } ,
143+ ]
144+ @validator . validate ( block_content )
145+
146+ assert_empty block_content . errors [ "social_media_links.0.social_media_service_name" . to_sym ]
147+ assert_includes block_content . errors [ "social_media_links.1.social_media_service_name" . to_sym ] , "must be unique"
148+ end
149+
150+ test "social media links are invalid when multiple 'Other' channels are provided, with no distinct titles" do
151+ block_content = SocialMediaLinksValidatorTestClass . new
152+ block_content . social_media_links = [
153+ { "social_media_service_name" => "Other" , "url" => "http://example.com/one" } ,
154+ { "social_media_service_name" => "Other" , "url" => "http://example.com/two" } ,
155+ ]
156+ @validator . validate ( block_content )
157+
158+ assert_includes block_content . errors [ "social_media_links.1.social_media_service_name" . to_sym ] , "must be unique"
159+ end
160+
161+ test "social media links are invalid and only display title uniqueness error, if multiple instances of the same channel are provided, and their titles match" do
162+ block_content = SocialMediaLinksValidatorTestClass . new
163+ block_content . social_media_links = [
164+ { "social_media_service_name" => "Facebook" , "title" => "Facebook" , "url" => "http://facebook.com/govuk" } ,
165+ { "social_media_service_name" => "Facebook" , "title" => "Facebook" , "url" => "http://facebook.com/govukpage2" } ,
166+ ]
167+ @validator . validate ( block_content )
168+
169+ assert_includes block_content . errors [ "social_media_links.1.title" . to_sym ] , "must be unique"
170+ assert block_content . errors [ "social_media_links.1.social_media_service_name" . to_sym ] . empty?
171+ end
172+
173+ test "social media links are valid when multiple instances of the same channel are provided with different titles and different URLs" do
174+ block_content = SocialMediaLinksValidatorTestClass . new
175+ block_content . social_media_links = [
176+ { "social_media_service_name" => "Facebook" , "title" => "Facebook Corporate" , "url" => "http://example.com/one" } ,
177+ { "social_media_service_name" => "Facebook" , "title" => "Facebook Fun" , "url" => "http://example.com/two" } ,
121178 ]
122179 @validator . validate ( block_content )
123180
0 commit comments