1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
# encoding: UTF-8
require 'test_helper'
adapter = ENV['ADAPTER'] || 'activerecord'
require File.join(File.expand_path(File.dirname(__FILE__)), "acts_as_url/adapter/#{adapter}.rb")
class ActsAsUrlIntegrationTest < Test::Unit::TestCase
include AdapterSpecificTestBehaviors
def test_should_create_url
@doc = Document.create(title: "Let's Make a Test Title, <em>Okay</em>?")
assert_equal "lets-make-a-test-title-okay", @doc.url
end
def test_should_create_unique_url
@doc = Document.create(title: "Unique")
@other_doc = Document.create(title: "Unique")
assert_equal "unique", @doc.url
assert_equal "unique-1", @other_doc.url
end
def test_should_allow_custom_duplicates
sequence = Enumerator.new { |enum| loop { enum.yield 12345 } }
Document.class_eval do
acts_as_url :title, duplicate_sequence: sequence
end
@doc = Document.create(title: "New")
@other_doc = Document.create(title: "New")
assert_equal "new-document", @doc.url
assert_equal "new-document-12345", @other_doc.url
end
def test_should_restart_duplicate_sequence_each_time
sequence = Enumerator.new do |enum|
n = 1
loop do
enum.yield n
n += 1
end
end
Document.class_eval do
acts_as_url :title, duplicate_sequence: sequence
end
@doc = Document.create(title: "Unique")
@other_doc = Document.create(title: "Unique")
@third_doc = Document.create(title: "Another")
@fourth_doc = Document.create(title: "Another")
assert_equal "unique", @doc.url
assert_equal "unique-1", @other_doc.url
assert_equal "another", @third_doc.url
assert_equal "another-1", @fourth_doc.url
end
def test_should_avoid_blacklist
@doc = Document.create(title: "New")
@other_doc = Document.create(title: "new")
assert_equal "new-document", @doc.url
assert_equal "new-document-1", @other_doc.url
end
def test_should_allow_customizing_blacklist
Document.class_eval do
# Un-blacklisting 'new' isn't advisable
acts_as_url :title, blacklist: %w{special}
end
@doc = Document.create(title: "New")
@other_doc = Document.create(title: "Special")
assert_equal 'new', @doc.url
assert_equal 'special-document', @other_doc.url
end
def test_should_allow_customizing_blacklist_policy
Document.class_eval do
acts_as_url :title, blacklist_policy: Proc.new(){|instance, url|
"#{url}-customized"
}
end
@doc = Document.create(title: "New")
@other_doc = Document.create(title: "New")
assert_equal 'new-customized', @doc.url
assert_equal 'new-customized-1', @other_doc.url
end
def test_should_create_unique_url_when_partial_url_already_exists
@doc = Document.create(title: "House Farms")
@other_doc = Document.create(title: "House Farm")
assert_equal "house-farms", @doc.url
assert_equal "house-farm", @other_doc.url
end
def test_should_not_sync_url_by_default
@doc = Document.create(title: "Stable as Stone")
@original_url = @doc.url
adapter_specific_update @doc, title: "New Unstable Madness"
assert_equal @original_url, @doc.url
end
def test_should_allow_syncing_url
Document.class_eval do
acts_as_url :title, sync_url: true
end
@doc = Document.create(title: "Original")
@original_url = @doc.url
adapter_specific_update @doc, title: "New and Improved"
assert_not_equal @original_url, @doc.url
end
def test_should_not_increment_count_on_repeated_saves
Document.class_eval do
acts_as_url :title, sync_url: true
end
@doc = Document.create(title: "Continuous or Constant")
assert_equal "continuous-or-constant", @doc.url
5.times do |n|
@doc.save!
assert_equal "continuous-or-constant", @doc.url
end
end
def test_should_allow_allowing_duplicate_url
Document.class_eval do
acts_as_url :title, allow_duplicates: true
end
@doc = Document.create(title: "I am not a clone")
@other_doc = Document.create(title: "I am not a clone")
assert_equal @doc.url, @other_doc.url
end
def test_should_allow_scoping_url_uniqueness
Document.class_eval do
acts_as_url :title, scope: :other
end
@doc = Document.create(title: "Mocumentary", other: "I don't care if I'm unique for some reason")
@other_doc = Document.create(title: "Mocumentary", other: "Me either")
assert_equal @doc.url, @other_doc.url
end
def test_should_still_create_unique_urls_if_scoped_attribute_is_the_same
Document.class_eval do
acts_as_url :title, scope: :other
end
@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique")
@other_doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique")
assert_not_equal @doc.url, @other_doc.url
end
def test_should_allow_multiple_scopes
Document.class_eval do
acts_as_url :title, scope: [:other, :another]
end
@doc = Document.create(title: "Mocumentary", other: "I don't care if I'm unique for some reason",
another: "Whatever")
@other_doc = Document.create(title: "Mocumentary", other: "Me either", another: "Whatever")
assert_equal @doc.url, @other_doc.url
end
def test_should_only_create_unique_urls_for_multiple_scopes_if_both_attributes_are_same
Document.class_eval do
acts_as_url :title, scope: [:other, :another]
end
@doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: "Whatever")
@other_doc = Document.create(title: "Mocumentary", other: "Suddenly, I care if I'm unique",
another: "Whatever")
assert_not_equal @doc.url, @other_doc.url
end
def test_should_allow_setting_url_attribute
Document.class_eval do
# Manually undefining the url method on Document which, in a real class not reused for tests,
# would never have been defined to begin with.
remove_method :url
acts_as_url :title, url_attribute: :other
end
@doc = Document.create(title: "Anything at This Point")
assert_equal "anything-at-this-point", @doc.other
assert_nil @doc.url
ensure
Document.class_eval do
# Manually undefining the other method on Document for the same reasons as before
remove_method :other
end
end
def test_should_allow_updating_url_only_when_blank
Document.class_eval do
acts_as_url :title, only_when_blank: true
end
@string = 'the-url-of-concrete'
@doc = Document.create(title: "Stable as Stone", url: @string)
assert_equal @string, @doc.url
@other_doc = Document.create(title: "Stable as Stone")
assert_equal 'stable-as-stone', @other_doc.url
end
def test_should_mass_initialize_urls
@doc = Document.create(title: "Initial")
@other_doc = Document.create(title: "Subsequent")
adapter_specific_update @doc, url: nil
adapter_specific_update @other_doc, url: nil
# Just making sure this got unset before the real test
assert_nil @doc.url
assert_nil @other_doc.url
Document.initialize_urls
@doc.reload
@other_doc.reload
assert_equal "initial", @doc.url
assert_equal "subsequent", @other_doc.url
end
def test_should_mass_initialize_urls_with_custom_url_attribute
Document.class_eval do
# Manually undefining the url method on Document which, in a real class not reused for tests,
# would never have been defined to begin with.
remove_method :url
acts_as_url :title, url_attribute: :other
end
@doc = Document.create(title: "Initial")
@other_doc = Document.create(title: "Subsequent")
adapter_specific_update @doc, other: nil
adapter_specific_update @other_doc, other: nil
# Just making sure this got unset before the real test
assert_nil @doc.other
assert_nil @other_doc.other
Document.initialize_urls
@doc.reload
@other_doc.reload
assert_equal "initial", @doc.other
assert_equal "subsequent", @other_doc.other
ensure
Document.class_eval do
# Manually undefining the other method on Document for the same reasons as before
remove_method :other
end
end
def test_should_mass_initialize_empty_string_urls
@doc = Document.create(title: "Initial")
@other_doc = Document.create(title: "Subsequent")
adapter_specific_update @doc, url: ''
adapter_specific_update @other_doc, url: ''
# Just making sure this got unset before the real test
assert_equal '', @doc.url
assert_equal '', @other_doc.url
Document.initialize_urls
@doc.reload
@other_doc.reload
assert_equal "initial", @doc.url
assert_equal "subsequent", @other_doc.url
end
def test_should_allow_using_custom_method_for_generating_url
Document.class_eval do
acts_as_url :non_attribute_method
def non_attribute_method
"#{title} got massaged"
end
end
@doc = Document.create(title: "Title String")
assert_equal "title-string-got-massaged", @doc.url
ensure
Document.class_eval do
# Manually undefining method that isn't defined on Document by default
remove_method :non_attribute_method
end
end
def test_should_allow_customizing_duplicate_count_separator
Document.class_eval do
acts_as_url :title, duplicate_count_separator: "---"
end
@doc = Document.create(title: "Unique")
@other_doc = Document.create(title: "Unique")
assert_equal "unique", @doc.url
assert_equal "unique---1", @other_doc.url
end
def test_should_only_update_url_if_url_attribute_is_valid
Document.class_eval do
acts_as_url :title, sync_url: true
end
add_validation_on_document_title
@doc = Document.create(title: "Valid Record", other: "Present")
assert_equal "valid-record", @doc.url
@doc.title = nil
assert_equal false, @doc.valid?
assert_equal "valid-record", @doc.url
ensure
remove_validation_on_document_title
end
def test_should_allow_customizing_url_limit
Document.class_eval do
acts_as_url :title, limit: 13
end
@doc = Document.create(title: "I am much too long")
assert_equal "i-am-much-too", @doc.url
end
def test_handling_duplicate_urls_with_limits
Document.class_eval do
acts_as_url :title, limit: 13
end
@doc = Document.create(title: "I am much too long and also duplicated")
assert_equal "i-am-much-too", @doc.url
@other_doc = Document.create(title: "I am much too long and also duplicated")
assert_equal "i-am-much-too-1", @other_doc.url
end
def test_should_allow_excluding_specific_values_from_being_run_through_to_url
Document.class_eval do
acts_as_url :title, exclude: ["_So_Fucking_Special"]
end
@doc = Document.create(title: "_So_Fucking_Special")
assert_equal "_So_Fucking_Special", @doc.url
@doc_2 = Document.create(title: "But I'm a creep")
assert_equal "but-im-a-creep", @doc_2.url
end
def test_should_allow_not_forcing_downcasing
Document.class_eval do
acts_as_url :title, force_downcase: false
end
@doc = Document.create(title: "I have CAPS!")
assert_equal "I-have-CAPS", @doc.url
end
def test_should_allow_alternate_whitespace_replacements
Document.class_eval do
acts_as_url :title, replace_whitespace_with: "~"
end
@doc = Document.create(title: "now with tildes")
assert_equal "now~with~tildes", @doc.url
end
def test_should_allow_enforcing_uniqueness_on_sti_base_class
STIBaseDocument.class_eval do
acts_as_url :title, enforce_uniqueness_on_sti_base_class: true
end
@doc = STIChildDocument.create(title: "Unique")
assert_equal "unique", @doc.url
@doc_2 = AnotherSTIChildDocument.create(title: "Unique")
assert_equal "unique-1", @doc_2.url
end
def test_should_strip_slashes_by_default
Document.class_eval do
acts_as_url :title
end
@doc = Document.create(title: "a b/c d")
assert_equal "a-b-slash-c-d", @doc.url
end
def test_should_allow_slashes_to_be_allowed
Document.class_eval do
acts_as_url :title, allow_slash: true
end
@doc = Document.create(title: "a b/c d")
assert_equal "a-b/c-d", @doc.url
end
def test_should_truncate_words_by_default
Document.class_eval do
acts_as_url :title, limit: 20
end
@doc = Document.create(title: "title with many whole words")
assert_equal 'title-with-many-whol', @doc.url
end
def test_should_not_truncate_words
Document.class_eval do
acts_as_url :title, limit: 20, truncate_words: false
end
@doc = Document.create(title: "title with many whole words")
assert_equal 'title-with-many', @doc.url
end
def test_should_allow_overriding_url_taken_method
Document.class_eval do
acts_as_url :title, url_taken_method: :url_taken?
def url_taken?(url)
["unique", "unique-1", "unique-2"].include?(url)
end
end
@doc = Document.create(title: "unique")
assert_equal "unique-3", @doc.url
end
end
|