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
|
|
|
|
# Trabajamos con el sitio de sutty porque tiene artículos
|
|
|
|
#
|
|
|
|
# TODO: Cambiar a skel cuando publiquemos los códigos y privacidad
|
|
|
|
@site = create :site, name: 'sutty.nl'
|
|
|
|
@site.read
|
|
|
|
@post = @site.posts.sample
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
# @site.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede acceder a los valores' do
|
|
|
|
assert @site.posts.size.positive?
|
|
|
|
|
|
|
|
assert @post.categories.values.size.positive?
|
|
|
|
assert @post.tags.values.size.positive?
|
|
|
|
assert @post.title.size.positive?
|
|
|
|
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
|
|
|
|
# TODO: cuando esté con git, solo aplicar git reset
|
|
|
|
tmp = File.join(Rails.root, 'tmp', 'eliminar.md')
|
2019-08-08 18:28:23 +00:00
|
|
|
FileUtils.cp @post.path.absolute, tmp
|
2019-08-06 23:17:29 +00:00
|
|
|
|
|
|
|
assert @post.destroy
|
2019-08-08 18:28:23 +00:00
|
|
|
assert_not File.exist?(@post.path.absolute)
|
2019-08-06 23:17:29 +00:00
|
|
|
assert_not @site.posts.include?(@post)
|
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
FileUtils.mv tmp, @post.path.absolute
|
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
|
2019-08-08 18:28:23 +00:00
|
|
|
@post.read
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
# 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-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
|
|
|
|
|
|
|
|
test 'se pueden guardar los cambios' do
|
2019-08-07 15:10:14 +00:00
|
|
|
title = SecureRandom.hex
|
|
|
|
@post.title.value = title
|
|
|
|
@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)
|
|
|
|
document.read
|
|
|
|
|
|
|
|
assert document.data['categories'].include?(title)
|
|
|
|
assert_equal title, document.data['title']
|
|
|
|
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 'attribute_name' do
|
|
|
|
assert_equal :hola, @post.send(:attribute_name, :hola)
|
|
|
|
assert_equal :hola, @post.send(:attribute_name, :hola?)
|
|
|
|
assert_equal :hola, @post.send(:attribute_name, :hola_was)
|
|
|
|
assert_equal :hola, @post.send(:attribute_name, :hola_changed?)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede cambiar el slug' do
|
|
|
|
assert_equal @post.slug_was, @post.slug.value
|
|
|
|
assert_not @post.slug_changed?
|
|
|
|
assert @post.slug.valid?
|
|
|
|
|
|
|
|
ex_slug = @post.slug.value
|
|
|
|
@post.slug.value = SecureRandom.hex
|
|
|
|
|
|
|
|
assert_not_equal ex_slug, @post.slug.value
|
|
|
|
assert_equal ex_slug, @post.slug_was
|
|
|
|
assert @post.slug_changed?
|
|
|
|
assert @post.slug.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede cambiar la fecha' do
|
|
|
|
assert_equal @post.date_was, @post.date.value
|
|
|
|
assert_not @post.date_changed?
|
|
|
|
assert @post.date.valid?
|
|
|
|
|
|
|
|
ex_date = @post.date.value
|
|
|
|
@post.date.value = 2.days.ago
|
|
|
|
|
|
|
|
assert_not_equal ex_date, @post.date.value
|
|
|
|
assert_equal ex_date, @post.date_was
|
|
|
|
assert @post.date_changed?
|
|
|
|
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
|
|
|
|
hoy = Date.today.to_time
|
|
|
|
path_was = @post.path.absolute
|
|
|
|
@post.slug.value = 'test'
|
|
|
|
@post.date.value = hoy
|
|
|
|
|
|
|
|
assert @post.path_changed?
|
|
|
|
assert_equal "_es/#{hoy.strftime('%F')}-test.markdown",
|
|
|
|
@post.path.relative
|
|
|
|
|
|
|
|
assert @post.save
|
|
|
|
assert_equal "_es/#{hoy.strftime('%F')}-test.markdown",
|
|
|
|
@post.document.relative_path
|
|
|
|
assert_not File.exist?(path_was)
|
|
|
|
assert File.exist?(@post.path.absolute)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no podemos pisar otros archivos' do
|
|
|
|
# Elegir un post al azar que no sea el que usamos
|
|
|
|
# rubocop:disable Lint/Loop
|
|
|
|
begin
|
|
|
|
post = @site.posts.sample
|
|
|
|
end until post != @post
|
|
|
|
# rubocop:enable Lint/Loop
|
|
|
|
|
|
|
|
@post.slug.value = post.slug.value
|
|
|
|
@post.date.value = post.date.value
|
|
|
|
|
|
|
|
assert_not @post.save
|
|
|
|
assert File.exist?(@post.path.absolute)
|
|
|
|
assert File.exist?(@post.path_was)
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|