2023-10-06 14:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class MetadataDateTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@site = build(:site)
|
|
|
|
@document = build(:document, site: @site.jekyll)
|
|
|
|
@name = SecureRandom.hex
|
|
|
|
@layout = build(:layout, site: @site)
|
|
|
|
@metadata = MetadataDate.new(site: @site, document: @document, name: @name, type: 'date', layout: @layout)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Una fecha al azar en el pasado o en el futuro
|
|
|
|
def random_value
|
|
|
|
Date.today.public_send(%i[- +].sample, (0..365).to_a.sample.days)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se guarda en el encabezado' do
|
|
|
|
assert @metadata.front_matter?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'por defecto es la fecha de hoy' do
|
|
|
|
assert_equal Date.today, @metadata.default_value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'puede traer el valor desde el documento' do
|
|
|
|
@document.data[@name] = random_value
|
|
|
|
|
|
|
|
assert_equal @document[@name], @metadata.document_value
|
|
|
|
assert @metadata.save
|
|
|
|
assert_equal @document[@name], @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'al cambiar el valor podemos obtenerlo' do
|
|
|
|
@metadata.value = value_was = random_value
|
|
|
|
@metadata.value = random_value
|
|
|
|
|
|
|
|
assert_equal value_was, @metadata.value_was
|
|
|
|
assert_not_equal value_was, @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'pueden tener un valor por defecto desde el layout' do
|
|
|
|
default = random_value
|
|
|
|
|
2023-10-06 14:53:11 +00:00
|
|
|
@layout = build(:layout, site: @site, metadata: { @name => { 'default' => { I18n.locale.to_s => default } } })
|
2023-10-06 14:52:19 +00:00
|
|
|
@metadata = MetadataDate.new(site: @site, document: @document, name: @name, type: 'date', layout: @layout)
|
|
|
|
|
|
|
|
assert_equal default, @metadata.default_value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no se pueden indexar' do
|
|
|
|
assert_not @metadata.indexable?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'son públicos por defecto' do
|
|
|
|
assert_not @metadata.private?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden hacer privados' do
|
2023-10-06 14:53:11 +00:00
|
|
|
@layout = build(:layout, site: @site, metadata: { @name => { 'private' => true } })
|
2023-10-06 14:52:19 +00:00
|
|
|
@metadata = MetadataDate.new(site: @site, document: @document, name: @name, type: 'date', layout: @layout)
|
|
|
|
|
|
|
|
assert @metadata.private?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se les puede asignar un valor' do
|
|
|
|
@metadata.value = value = random_value
|
|
|
|
|
|
|
|
assert_equal value, @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se le puede asignar una fecha como string' do
|
|
|
|
value = random_value
|
|
|
|
@metadata.value = value.to_s
|
|
|
|
|
|
|
|
assert @metadata.save
|
|
|
|
assert_equal value, @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'el valor asignado no puede tener espacios adelante ni atrás' do
|
|
|
|
value = random_value
|
|
|
|
@metadata.value = " #{value}\n\n"
|
|
|
|
|
|
|
|
assert @metadata.save
|
|
|
|
assert_equal value, @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no pueden tener html' do
|
|
|
|
content = random_value
|
|
|
|
|
|
|
|
@metadata.value = value = "<html>#{content}</html>"
|
|
|
|
|
|
|
|
assert @metadata.save
|
|
|
|
assert_not_equal value, @metadata.value
|
|
|
|
assert_equal content, @metadata.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no se pueden asignar fechas muy en el futuro' do
|
|
|
|
@metadata.value = '10000-01-01'
|
|
|
|
|
|
|
|
assert_not @metadata.valid?
|
|
|
|
end
|
|
|
|
end
|