mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-24 12:46:22 +00:00
141 lines
4 KiB
Ruby
141 lines
4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'test_helper'
|
|
|
|
class MetadataPredefinedArrayTest < ActiveSupport::TestCase
|
|
setup do
|
|
@site = build(:site)
|
|
@document = build(:document, site: @site.jekyll)
|
|
@name = SecureRandom.hex
|
|
@locale = I18n.available_locales.sample.to_s
|
|
@layout = build(:layout, site: @site, metadata: {
|
|
@name => {
|
|
'values' => random_values
|
|
}
|
|
})
|
|
@metadata = MetadataPredefinedArray.new(site: @site, document: @document, name: @name, type: 'predefined_array',
|
|
layout: @layout)
|
|
end
|
|
|
|
def random_values
|
|
(1..100).to_a.sample.times.to_h do
|
|
[
|
|
SecureRandom.hex,
|
|
I18n.available_locales.map(&:to_s).to_h do |l|
|
|
[l, SecureRandom.hex]
|
|
end
|
|
]
|
|
end
|
|
end
|
|
|
|
def random_value
|
|
@metadata.values.values.sample((2..(@metadata.values.values.size)).to_a.sample)
|
|
end
|
|
|
|
test 'se guarda en el encabezado' do
|
|
assert @metadata.front_matter?
|
|
end
|
|
|
|
test 'por defecto es un array vacía' do
|
|
assert_equal [], @metadata.default_value
|
|
end
|
|
|
|
test 'los valores posibles vienen desde el layout' do
|
|
assert_equal @layout.metadata.dig(@name, 'values').keys, @metadata.values.values
|
|
end
|
|
|
|
test 'no puede estar vacía si es obligatoria' do
|
|
@layout = build(:layout, site: @site, metadata: { @name => { 'default' => { 'required' => true } } })
|
|
@metadata = MetadataPredefinedArray.new(site: @site, document: @document, name: @name, type: 'string', layout: @layout,
|
|
required: true)
|
|
|
|
assert @metadata.required
|
|
assert @metadata.empty?
|
|
assert_not @metadata.valid?
|
|
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
|
|
|
|
@layout = build(:layout, site: @site, metadata: { @name => { 'default' => { I18n.locale.to_s => default } } })
|
|
@metadata = MetadataArray.new(site: @site, document: @document, name: @name, type: 'array', layout: @layout)
|
|
|
|
assert_equal default, @metadata.default_value
|
|
end
|
|
|
|
test 'se pueden indexar' do
|
|
assert @metadata.indexable?
|
|
end
|
|
|
|
test 'son públicos por defecto' do
|
|
assert_not @metadata.private?
|
|
end
|
|
|
|
test 'no se pueden hacer privados' do
|
|
@layout = build(:layout, site: @site, metadata: { @name => { 'private' => true } })
|
|
@metadata = MetadataArray.new(site: @site, document: @document, name: @name, type: 'predefined_array',
|
|
layout: @layout)
|
|
|
|
assert_not @metadata.private?
|
|
end
|
|
|
|
test 'se les puede asignar un valor' do
|
|
@metadata.value = value = random_value
|
|
|
|
assert_equal value, @metadata.value
|
|
end
|
|
|
|
test 'el valor asignado no puede tener espacios adelante ni atrás' do
|
|
@metadata.value = value = [" #{random_value.sample}\n\n"]
|
|
|
|
assert @metadata.save
|
|
assert_equal value.map(&:strip), @metadata.value
|
|
end
|
|
|
|
test 'no pueden tener html' do
|
|
content = random_value.sample
|
|
|
|
@metadata.value = value = ["<html>#{content}</html>"]
|
|
|
|
assert @metadata.save
|
|
assert_not_equal value, @metadata.value
|
|
assert_equal [content], @metadata.value
|
|
end
|
|
|
|
test 'los valores vacíos son eliminados' do
|
|
@metadata.value = [
|
|
nil,
|
|
'',
|
|
' '
|
|
]
|
|
|
|
@metadata.save
|
|
|
|
assert @metadata.value.empty?
|
|
assert @metadata.empty?
|
|
end
|
|
|
|
test 'solo se permiten los valores predefinidos' do
|
|
valid_values = random_value
|
|
@metadata.value = valid_values + ((1..100).to_a.sample.times.map { SecureRandom.hex })
|
|
|
|
assert @metadata.save
|
|
assert_equal valid_values, @metadata.value
|
|
end
|
|
end
|