|
19 | 19 | end |
20 | 20 |
|
21 | 21 | describe 'PATCH /:locale/.../host/platforms/:id' do |
| 22 | + let(:host_platform) { BetterTogether::Platform.find_by(host: true) } |
| 23 | + |
22 | 24 | # rubocop:todo RSpec/MultipleExpectations |
23 | 25 | it 'updates settings and redirects' do # rubocop:todo RSpec/MultipleExpectations |
24 | 26 | # rubocop:enable RSpec/MultipleExpectations |
25 | | - host_platform = BetterTogether::Platform.find_by(host: true) |
26 | 27 | patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
27 | 28 | platform: { url: host_platform.url, time_zone: host_platform.time_zone, requires_invitation: true } |
28 | 29 | } |
29 | 30 | expect(response).to have_http_status(:see_other) |
30 | 31 | follow_redirect! |
31 | 32 | expect(response).to have_http_status(:ok) |
32 | 33 | end |
| 34 | + |
| 35 | + context 'when updating CSS block' do |
| 36 | + context 'when platform has no existing CSS block' do # rubocop:todo RSpec/NestedGroups |
| 37 | + before do |
| 38 | + # Ensure platform starts without a CSS block |
| 39 | + host_platform.blocks.where(type: 'BetterTogether::Content::Css').destroy_all |
| 40 | + host_platform.reload |
| 41 | + end |
| 42 | + |
| 43 | + it 'creates a new CSS block with content' do # rubocop:todo RSpec/MultipleExpectations |
| 44 | + css_content = '.my-custom-class { color: blue; }' |
| 45 | + |
| 46 | + expect do |
| 47 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 48 | + platform: { |
| 49 | + url: host_platform.url, |
| 50 | + time_zone: host_platform.time_zone, |
| 51 | + css_block_attributes: { |
| 52 | + type: 'BetterTogether::Content::Css', |
| 53 | + identifier: 'platform-custom-css', |
| 54 | + "content_#{locale}": css_content |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + end.to change { host_platform.reload.css_block.present? }.from(false).to(true) |
| 59 | + |
| 60 | + expect(response).to have_http_status(:see_other) |
| 61 | + |
| 62 | + css_block = host_platform.reload.css_block |
| 63 | + expect(css_block).to be_present |
| 64 | + expect(css_block.content).to eq(css_content) |
| 65 | + expect(css_block.identifier).to eq('platform-custom-css') |
| 66 | + expect(css_block.protected).to be(true) |
| 67 | + end |
| 68 | + |
| 69 | + it 'creates CSS block with complex real-world CSS' do # rubocop:todo RSpec/MultipleExpectations |
| 70 | + complex_css = <<~CSS |
| 71 | + .notification form[action*="mark_as_read"] .btn[type="submit"] { z-index: 1200; } |
| 72 | + .card.journey-stage > .card-body { max-height: 50vh; } |
| 73 | + @media only screen and (min-width: 768px) { |
| 74 | + .hero-heading { font-size: 3em; } |
| 75 | + } |
| 76 | + CSS |
| 77 | + |
| 78 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 79 | + platform: { |
| 80 | + url: host_platform.url, |
| 81 | + time_zone: host_platform.time_zone, |
| 82 | + css_block_attributes: { |
| 83 | + type: 'BetterTogether::Content::Css', |
| 84 | + identifier: 'platform-custom-css', |
| 85 | + "content_#{locale}": complex_css |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + expect(response).to have_http_status(:see_other) |
| 91 | + |
| 92 | + css_block = host_platform.reload.css_block |
| 93 | + expect(css_block.content).to eq(complex_css) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + context 'when platform has existing CSS block' do # rubocop:todo RSpec/NestedGroups |
| 98 | + let(:existing_css_block) do |
| 99 | + create(:better_together_content_css, |
| 100 | + identifier: 'existing-platform-css', |
| 101 | + content_text: '.old-class { color: red; }', |
| 102 | + protected: true) |
| 103 | + end |
| 104 | + |
| 105 | + before do |
| 106 | + # Associate existing CSS block with platform |
| 107 | + host_platform.platform_blocks.create!(block: existing_css_block) |
| 108 | + host_platform.reload |
| 109 | + end |
| 110 | + |
| 111 | + it 'updates existing CSS block content' do # rubocop:todo RSpec/MultipleExpectations |
| 112 | + new_css_content = '.updated-class { color: green; }' |
| 113 | + |
| 114 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 115 | + platform: { |
| 116 | + url: host_platform.url, |
| 117 | + time_zone: host_platform.time_zone, |
| 118 | + css_block_attributes: { |
| 119 | + id: existing_css_block.id, |
| 120 | + type: 'BetterTogether::Content::Css', |
| 121 | + identifier: existing_css_block.identifier, |
| 122 | + "content_#{locale}": new_css_content |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + expect(response).to have_http_status(:see_other) |
| 128 | + |
| 129 | + existing_css_block.reload |
| 130 | + expect(existing_css_block.content).to eq(new_css_content) |
| 131 | + end |
| 132 | + |
| 133 | + it 'preserves CSS block ID when updating' do # rubocop:todo RSpec/MultipleExpectations |
| 134 | + original_id = existing_css_block.id |
| 135 | + new_css_content = '.another-class { font-size: 14px; }' |
| 136 | + |
| 137 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 138 | + platform: { |
| 139 | + url: host_platform.url, |
| 140 | + time_zone: host_platform.time_zone, |
| 141 | + css_block_attributes: { |
| 142 | + id: existing_css_block.id, |
| 143 | + type: 'BetterTogether::Content::Css', |
| 144 | + identifier: existing_css_block.identifier, |
| 145 | + "content_#{locale}": new_css_content |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + expect(response).to have_http_status(:see_other) |
| 151 | + |
| 152 | + css_block = host_platform.reload.css_block |
| 153 | + expect(css_block.id).to eq(original_id) |
| 154 | + expect(css_block.content).to eq(new_css_content) |
| 155 | + end |
| 156 | + |
| 157 | + it 'handles CSS with special characters and quotes' do # rubocop:todo RSpec/MultipleExpectations |
| 158 | + css_with_quotes = <<~CSS |
| 159 | + .notification form[action*="mark_as_read"] .btn[type="submit"] { |
| 160 | + z-index: 1200; |
| 161 | + position: relative; |
| 162 | + } |
| 163 | + .trix-content a[href]:not([href*="example.com"])::after { |
| 164 | + content: "\\f35d"; |
| 165 | + font-family: "Font Awesome 6 Free"; |
| 166 | + } |
| 167 | + CSS |
| 168 | + |
| 169 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 170 | + platform: { |
| 171 | + url: host_platform.url, |
| 172 | + time_zone: host_platform.time_zone, |
| 173 | + css_block_attributes: { |
| 174 | + id: existing_css_block.id, |
| 175 | + type: 'BetterTogether::Content::Css', |
| 176 | + identifier: existing_css_block.identifier, |
| 177 | + "content_#{locale}": css_with_quotes |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + expect(response).to have_http_status(:see_other) |
| 183 | + |
| 184 | + existing_css_block.reload |
| 185 | + expect(existing_css_block.content).to eq(css_with_quotes) |
| 186 | + # Verify special characters are preserved |
| 187 | + expect(existing_css_block.content).to include('[action*="mark_as_read"]') |
| 188 | + expect(existing_css_block.content).to include('content: "\\f35d"') |
| 189 | + end |
| 190 | + |
| 191 | + it 'clears CSS content when empty string submitted' do # rubocop:todo RSpec/MultipleExpectations |
| 192 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 193 | + platform: { |
| 194 | + url: host_platform.url, |
| 195 | + time_zone: host_platform.time_zone, |
| 196 | + css_block_attributes: { |
| 197 | + id: existing_css_block.id, |
| 198 | + type: 'BetterTogether::Content::Css', |
| 199 | + identifier: existing_css_block.identifier, |
| 200 | + "content_#{locale}": '' |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + expect(response).to have_http_status(:see_other) |
| 206 | + |
| 207 | + existing_css_block.reload |
| 208 | + expect(existing_css_block.content).to be_blank |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + context 'when CSS block update fails validation' do # rubocop:todo RSpec/NestedGroups |
| 213 | + let(:existing_css_block) do |
| 214 | + create(:better_together_content_css, |
| 215 | + identifier: 'existing-platform-css', |
| 216 | + content_text: '.old-class { color: red; }', |
| 217 | + protected: true) |
| 218 | + end |
| 219 | + |
| 220 | + before do |
| 221 | + host_platform.platform_blocks.create!(block: existing_css_block) |
| 222 | + host_platform.reload |
| 223 | + |
| 224 | + # Stub validation to force failure |
| 225 | + allow_any_instance_of(BetterTogether::Platform).to receive(:update).and_return(false) # rubocop:todo RSpec/AnyInstance |
| 226 | + end |
| 227 | + |
| 228 | + it 'renders edit form with unprocessable_content status' do # rubocop:todo RSpec/MultipleExpectations |
| 229 | + patch better_together.platform_path(locale:, id: host_platform.slug), params: { |
| 230 | + platform: { |
| 231 | + url: host_platform.url, |
| 232 | + time_zone: host_platform.time_zone, |
| 233 | + css_block_attributes: { |
| 234 | + id: existing_css_block.id, |
| 235 | + type: 'BetterTogether::Content::Css', |
| 236 | + identifier: existing_css_block.identifier, |
| 237 | + "content_#{locale}": '.new-class { color: blue; }' |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + expect(response).to have_http_status(:unprocessable_content) |
| 243 | + expect(response).to render_template(:edit) |
| 244 | + end |
| 245 | + end |
| 246 | + end |
33 | 247 | end |
34 | 248 | end |
0 commit comments