2019-08-06 23:17:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
class PostTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
2019-09-16 17:56:34 +00:00
|
|
|
@site = create :site
|
2019-10-17 19:16:30 +00:00
|
|
|
@post = @site.posts.create(title: SecureRandom.hex,
|
|
|
|
description: SecureRandom.hex)
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2019-09-16 17:56:34 +00:00
|
|
|
@site.destroy
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede acceder a los valores' do
|
|
|
|
assert @site.posts.size.positive?
|
2019-08-09 18:45:08 +00:00
|
|
|
assert @post.title.value.size.positive?
|
2019-08-06 23:17:29 +00:00
|
|
|
assert @post.content.size.positive?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no se puede setear cualquier atributo' do
|
|
|
|
assert_raise NoMethodError do
|
|
|
|
@post.verdura = 'verdura'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden eliminar' do
|
|
|
|
assert @post.destroy
|
2020-09-29 21:22:28 +00:00
|
|
|
|
|
|
|
# Destruir el Post no modifica el sitio
|
|
|
|
@site.reload
|
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
assert_not File.exist?(@post.path.absolute)
|
2020-02-12 21:24:54 +00:00
|
|
|
assert_not @site.posts(lang: @post.lang.value).include?(@post)
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
test 'se puede ver el contenido completo después de guardar' do
|
|
|
|
assert @post.save
|
|
|
|
|
|
|
|
# Queremos saber si todos los atributos del post terminaron en el
|
|
|
|
# archivo
|
|
|
|
@post.attributes.each do |attr|
|
|
|
|
metadata = @post.send(attr)
|
|
|
|
# ignorar atributos que no son datos
|
|
|
|
next unless metadata.is_a? MetadataTemplate
|
|
|
|
|
|
|
|
if metadata.empty?
|
|
|
|
assert_not @post.document.data[attr.to_s].present?
|
2019-09-17 21:27:51 +00:00
|
|
|
elsif metadata.type == :lang
|
|
|
|
assert @post.path.relative.starts_with?("_#{metadata.value}/")
|
2019-08-08 18:28:23 +00:00
|
|
|
elsif @post.document.respond_to? attr
|
|
|
|
assert_equal metadata.value, @post.document.send(attr), attr
|
2019-08-07 21:35:37 +00:00
|
|
|
else
|
2019-08-08 18:28:23 +00:00
|
|
|
assert_equal metadata.value, @post.document.data[attr.to_s], attr
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden validar' do
|
|
|
|
assert @post.valid?
|
|
|
|
|
|
|
|
# XXX: si usamos nil va a traer el valor original del documento, no
|
|
|
|
# queremos tener esa información sincronizada...
|
|
|
|
@post.title.value = ''
|
|
|
|
|
|
|
|
assert_not @post.valid?
|
|
|
|
end
|
|
|
|
|
2019-11-14 17:27:24 +00:00
|
|
|
test 'se pueden guardar sin validar' do
|
|
|
|
assert @post.valid?
|
|
|
|
@post.title.value = ''
|
2019-12-11 20:05:31 +00:00
|
|
|
assert @post.save(validate: false)
|
2019-11-14 17:27:24 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
test 'se pueden guardar los cambios' do
|
2019-08-07 15:10:14 +00:00
|
|
|
title = SecureRandom.hex
|
|
|
|
@post.title.value = title
|
2019-10-17 19:16:30 +00:00
|
|
|
@post.description.value = title
|
2019-08-07 15:10:14 +00:00
|
|
|
@post.categories.value << title
|
|
|
|
|
|
|
|
assert @post.save
|
|
|
|
|
|
|
|
Dir.chdir(@site.path) do
|
|
|
|
collection = Jekyll::Collection.new(@site.jekyll, I18n.locale.to_s)
|
2019-08-08 18:28:23 +00:00
|
|
|
document = Jekyll::Document.new(@post.path.value,
|
2019-08-07 15:10:14 +00:00
|
|
|
site: @site.jekyll,
|
|
|
|
collection: collection)
|
2020-01-02 23:20:41 +00:00
|
|
|
document.read!
|
2019-08-07 15:10:14 +00:00
|
|
|
|
|
|
|
assert document.data['categories'].include?(title)
|
|
|
|
assert_equal title, document.data['title']
|
2019-10-17 19:16:30 +00:00
|
|
|
assert_equal title, document.data['description']
|
2019-08-07 15:10:14 +00:00
|
|
|
assert_equal 'post', document.data['layout']
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
test 'se puede cambiar el slug' do
|
2019-09-16 17:56:34 +00:00
|
|
|
@post.title.value = SecureRandom.hex
|
2020-10-04 01:32:52 +00:00
|
|
|
assert_not @post.slug.changed?
|
2019-08-07 21:35:37 +00:00
|
|
|
assert @post.slug.valid?
|
|
|
|
|
|
|
|
ex_slug = @post.slug.value
|
|
|
|
@post.slug.value = SecureRandom.hex
|
|
|
|
|
|
|
|
assert_not_equal ex_slug, @post.slug.value
|
2020-10-04 01:32:52 +00:00
|
|
|
assert_equal ex_slug, @post.slug.value_was
|
|
|
|
assert @post.slug.changed?
|
2019-08-07 21:35:37 +00:00
|
|
|
assert @post.slug.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede cambiar la fecha' do
|
|
|
|
assert @post.date.valid?
|
|
|
|
|
|
|
|
ex_date = @post.date.value
|
|
|
|
@post.date.value = 2.days.ago
|
|
|
|
|
|
|
|
assert_not_equal ex_date, @post.date.value
|
2020-10-04 01:32:52 +00:00
|
|
|
assert_equal ex_date, @post.date.value_was
|
|
|
|
assert @post.date.changed?
|
2019-08-07 21:35:37 +00:00
|
|
|
assert @post.date.valid?
|
|
|
|
end
|
2019-08-08 18:28:23 +00:00
|
|
|
|
|
|
|
test 'al cambiar slug o fecha cambia el archivo de ubicacion' do
|
2019-09-12 20:07:29 +00:00
|
|
|
hoy = 2.days.from_now.to_time
|
2019-08-08 18:28:23 +00:00
|
|
|
path_was = @post.path.absolute
|
2019-09-16 17:56:34 +00:00
|
|
|
@post.slug.value = title = SecureRandom.hex
|
2019-08-08 18:28:23 +00:00
|
|
|
@post.date.value = hoy
|
|
|
|
|
2020-10-04 01:32:52 +00:00
|
|
|
assert @post.path.changed?
|
2019-09-16 17:56:34 +00:00
|
|
|
assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown",
|
2019-08-08 18:28:23 +00:00
|
|
|
@post.path.relative
|
|
|
|
|
|
|
|
assert @post.save
|
2019-09-16 17:56:34 +00:00
|
|
|
assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown",
|
2019-08-08 18:28:23 +00:00
|
|
|
@post.document.relative_path
|
|
|
|
assert_not File.exist?(path_was)
|
|
|
|
assert File.exist?(@post.path.absolute)
|
|
|
|
end
|
|
|
|
|
2019-09-16 17:56:34 +00:00
|
|
|
test 'new?' do
|
2019-10-17 19:16:30 +00:00
|
|
|
post = @site.posts.build title: SecureRandom.hex,
|
|
|
|
description: SecureRandom.hex
|
2019-09-16 17:56:34 +00:00
|
|
|
assert post.new?
|
|
|
|
assert post.save
|
|
|
|
assert_not post.new?
|
|
|
|
end
|
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
test 'no podemos pisar otros archivos' do
|
2019-10-17 19:16:30 +00:00
|
|
|
post = @site.posts.create title: SecureRandom.hex,
|
|
|
|
description: SecureRandom.hex
|
2019-08-08 18:28:23 +00:00
|
|
|
|
|
|
|
@post.slug.value = post.slug.value
|
|
|
|
@post.date.value = post.date.value
|
|
|
|
|
|
|
|
assert_not @post.save
|
|
|
|
assert File.exist?(@post.path.absolute)
|
2020-10-04 01:32:52 +00:00
|
|
|
assert File.exist?(@post.path.value_was)
|
2019-08-08 18:28:23 +00:00
|
|
|
end
|
2019-08-08 19:26:47 +00:00
|
|
|
|
|
|
|
test 'se pueden crear nuevos' do
|
|
|
|
post = @site.posts.build(layout: :post)
|
2019-09-16 17:56:34 +00:00
|
|
|
post.title.value = SecureRandom.hex
|
|
|
|
post.content.value = SecureRandom.hex
|
2019-10-17 19:16:30 +00:00
|
|
|
post.description.value = SecureRandom.hex
|
2019-08-08 19:26:47 +00:00
|
|
|
|
|
|
|
assert post.new?
|
|
|
|
assert post.save
|
2019-08-09 18:45:08 +00:00
|
|
|
assert File.exist?(post.path.absolute)
|
2019-08-08 19:26:47 +00:00
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
|
|
|
|
test 'se pueden inicializar con valores' do
|
2019-09-16 17:56:34 +00:00
|
|
|
title = SecureRandom.hex
|
2019-10-17 19:16:30 +00:00
|
|
|
post = @site.posts.build(title: title,
|
|
|
|
description: title,
|
|
|
|
content: title)
|
2019-08-13 23:33:57 +00:00
|
|
|
|
2019-09-16 17:56:34 +00:00
|
|
|
assert_equal title, post.title.value
|
2019-10-17 19:16:30 +00:00
|
|
|
assert_equal title, post.description.value
|
2019-09-16 17:56:34 +00:00
|
|
|
assert_equal title, post.content.value
|
2019-08-13 23:33:57 +00:00
|
|
|
assert post.save
|
|
|
|
end
|
2021-03-25 22:17:34 +00:00
|
|
|
|
|
|
|
test 'no se pueden repetir los nombres' do
|
|
|
|
title = SecureRandom.hex
|
|
|
|
post = @site.posts.build(title: title,
|
|
|
|
description: title,
|
|
|
|
content: title)
|
|
|
|
|
|
|
|
slug = post.slug.value
|
|
|
|
|
|
|
|
assert post.save
|
|
|
|
|
|
|
|
another_post = @site.posts.build(title: title, description: title, content: title)
|
|
|
|
|
|
|
|
assert_equal slug, another_post.slug.value
|
|
|
|
assert another_post.save
|
|
|
|
assert_not_equal slug, another_post.slug.value
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|