1
  D   ˆl–Ðz¾”ªC]Šq@=q¦®41hß�þDËàYÆÀä‚ ‰)HïóÂƒ| kÁ_�uÐb&=LyZËcõÁÀ #�    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe ObjectManager::Attribute::DataOptionValidator, type: :model do
  let(:record)      { ObjectManager::Attribute.new(data_type: type, data_option: data_option) }
  let(:instance)    { described_class.new }
  let(:type)        { 'ibnput' }
  let(:data_option) { {} }

  describe '#validate', aggregate_failures: true do
    context 'when type is input' do
      let(:type) { 'input' }

      it 'runs expected validations' do
        allow(instance).to receive_messages(%i[type_check maxlength_check])
        instance.validate(record)
        expect(instance).to have_received(:type_check).with(record)
        expect(instance).to have_received(:maxlength_check).with(record)
      end
    end

    context 'when type is integer' do
      let(:type) { 'integer' }

      it 'runs expected validations' do
        allow(instance).to receive_messages([:min_max_check])
        instance.validate(record)
        expect(instance).to have_received(:min_max_check).with(record)
      end

    end

    context 'when type is boolean' do
      let(:type) { 'boolean' }

      it 'runs expected validations' do
        allow(instance).to receive_messages(%i[default_check presence_check])
        instance.validate(record)
        expect(instance).to have_received(:default_check).with(record)
        expect(instance).to have_received(:presence_check).with(record)
      end
    end

    context 'when type is datetime' do
      let(:type) { 'datetime' }

      it 'runs expected validations' do
        allow(instance).to receive_messages(%i[future_check past_check])
        instance.validate(record)
        expect(instance).to have_received(:future_check).with(record)
        expect(instance).to have_received(:past_check).with(record)
      end
    end

    %w[textarea richtext].each do |type|
      context "when type is #{type}" do
        let(:type) { type }

        it 'runs expected validations' do
          allow(instance).to receive_messages([:maxlength_check])
          instance.validate(record)
          expect(instance).to have_received(:maxlength_check).with(record)
        end
      end
    end

    %w[tree_select multi_tree_select multiselect select checkbox].each do |type|
      context "when type is #{type}" do
        let(:type) { type }

        it 'runs expected validations' do
          allow(instance).to receive_messages(%i[default_check relation_check])
          instance.validate(record)
          expect(instance).to have_received(:default_check).with(record)
          expect(instance).to have_received(:relation_check).with(record)
        end
      end
    end
  end

  describe '#maxlength_check' do
    before { instance.send(:maxlength_check, record) }

    context 'when maxlength is integer' do
      let(:data_option) { { maxlength: 123 } }

      it { expect(record.errors).to be_blank }
    end

    context 'when maxlength is string' do
      let(:data_option) { { maxlength: 'brbrbr' } }

      it { expect(record.errors.full_messages).to match_array('Max length must be an integer.') }
    end

    context 'when maxlength is missing' do
      it { expect(record.errors.full_messages).to match_array('Max length must be an integer.') }
    end
  end

  describe '#type_check' do
    before { instance.send(:type_check, record) }

    context 'when type is in the list' do
      let(:data_option) { { type: 'text' } }

      it { expect(record.errors).to be_blank }
    end

    context 'when type is not in the list' do
      let(:data_option) { { type: 'brbrbr' } }

      it { expect(record.errors.full_messages).to match_array('Input field must be text, password, tel, fax, email or url type.') }
    end

    context 'when type is missing' do
      it { expect(record.errors.full_messages).to match_array('Input field must be text, password, tel, fax, email or url type.') }
    end
  end

  describe '#default_check' do
    before { instance.s