<commit_before>require_relative "../test_helper"

class ProductsBlockTest < ActiveSupport::TestCase

  def setup
    @block = ProductsBlock.new
    @product_category = fast_create(ProductCategory, :name => 'Products')
  end
  attr_reader :block

  should 'be inherit from block' do
    assert_kind_of Block, block
  end

  should 'provide default title' do
    assert_not_equal Block.new.default_title, ProductsBlock.new.default_title
  end

  should 'provide default description' do
    assert_not_equal Block.description, ProductsBlock.description
  end

  should "list owner products" do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.expects(:products).returns(enterprise.products)

    content = block.content

    assert_tag_in_string content, :content => 'Products'

    assert_tag_in_string content, :tag => 'li', :attributes => { :class => 'product' }, :descendant => { :tag => 'a', :content => /product one/ }
    assert_tag_in_string content, :tag => 'li', :attributes => { :class => 'product' }, :descendant => { :tag => 'a', :content => /product two/ }
  end

  should 'point to all products in footer' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    footer = block.footer

    assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products'
  end

  should 'list 4 random products by default' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product five', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    assert_equal 4, block.products.size
  end

  should 'list all products if less than 4 by default' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    assert_equal 3, block.products.size
  end


  should 'be able to set product_ids and have them listed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    p3 = create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    p4 = create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)
    p5 = create(Product, :enterprise => enterprise, :name => 'product five', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    block.product_ids = [p1, p3, p5].map(&:id)
    assert_equivalent [p1, p3, p5], block.products
  end

  should 'save product_ids' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block = ProductsBlock.new
    enterprise.boxes.first.blocks << block
    block.product_ids = [p1.id, p2.id]
    block.save!

    assert_equal [p1.id, p2.id], ProductsBlock.find(block.id).product_ids
  end

  should 'accept strings in product_ids but store integers' do
    block = ProductsBlock.new
    block.product_ids = [ '1', '2']
    assert_equal [1, 2], block.product_ids
  end

  should 'not repeat products' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    p3 = create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    p4 = create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)

    block = ProductsBlock.new
    enterprise.boxes.first.blocks << block
    block.save!

    4.times do # to keep a minimal chance of false positive, its random after all
      assert_equivalent [p1, p2, p3, p4], block.products
    end
  end

  should 'generate footer when enterprise has own hostname' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    enterprise.domains << Domain.new(:name => 'some<EMAIL>'); enterprise.save!
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    footer = block.footer

    assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products'
  end

  should 'display the default minor image if thumbnails were not processed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})

    block.expects(:products).returns(enterprise.products)

    content = block.content

    assert_tag_in_string content, :tag => 'a', :attributes => { :style => /image-loading-minor.png/ }
  end

  should 'display the thumbnail image if thumbnails were processed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})

    process_delayed_job_queue
    block.expects(:products).returns(enterprise.products.reload)

    content = block.content
    assert_tag_in_string content, :tag => 'a', :attributes => { :style => /rails_minor.png/ }
  end

end<commit_msg>Fix ProductsBlock unit tests and view<commit_after>require_relative "../test_helper"

class ProductsBlockTest < ActiveSupport::TestCase

  def setup
    @block = ProductsBlock.new
    @product_category = fast_create(ProductCategory, :name => 'Products')
  end
  attr_reader :block

  should 'be inherit from block' do
    assert_kind_of Block, block
  end

  should 'provide default title' do
    assert_not_equal Block.new.default_title, ProductsBlock.new.default_title
  end

  should 'provide default description' do
    assert_not_equal Block.description, ProductsBlock.description
  end

  should 'point to all products in footer' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    footer = block.footer

    assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products'
  end

  should 'list 4 random products by default' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product five', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    assert_equal 4, block.products.size
  end

  should 'list all products if less than 4 by default' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    assert_equal 3, block.products.size
  end


  should 'be able to set product_ids and have them listed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    p3 = create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    p4 = create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)
    p5 = create(Product, :enterprise => enterprise, :name => 'product five', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    block.product_ids = [p1, p3, p5].map(&:id)
    assert_equivalent [p1, p3, p5], block.products
  end

  should 'save product_ids' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block = ProductsBlock.new
    enterprise.boxes.first.blocks << block
    block.product_ids = [p1.id, p2.id]
    block.save!

    assert_equal [p1.id, p2.id], ProductsBlock.find(block.id).product_ids
  end

  should 'accept strings in product_ids but store integers' do
    block = ProductsBlock.new
    block.product_ids = [ '1', '2']
    assert_equal [1, 2], block.product_ids
  end

  should 'not repeat products' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    p1 = create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    p2 = create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)
    p3 = create(Product, :enterprise => enterprise, :name => 'product three', :product_category => @product_category)
    p4 = create(Product, :enterprise => enterprise, :name => 'product four', :product_category => @product_category)

    block = ProductsBlock.new
    enterprise.boxes.first.blocks << block
    block.save!

    4.times do # to keep a minimal chance of false positive, its random after all
      assert_equivalent [p1, p2, p3, p4], block.products
    end
  end

  should 'generate footer when enterprise has own hostname' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    enterprise.domains << Domain.new(:name => 'sometest.com'); enterprise.save!
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.stubs(:owner).returns(enterprise)

    footer = block.footer

    assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products'
  end


end

require 'boxes_helper'
require 'block_helper'

class ProductsBlockViewTest < ActionView::TestCase
  include BoxesHelper

  ActionView::Base.send :include, BlockHelper

  def setup
    @block = ProductsBlock.new
    @product_category = fast_create(ProductCategory, :name => 'Products')
  end
  attr_reader :block

  should "list owner products" do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product one', :product_category => @product_category)
    create(Product, :enterprise => enterprise, :name => 'product two', :product_category => @product_category)

    block.expects(:products).returns(enterprise.products)

    content = render_block_content(block)

    assert_tag_in_string content, :content => 'Products'

    assert_tag_in_string content, :tag => 'li', :attributes => { :class => 'product' }, :descendant => { :tag => 'a', :content => /product one/ }
    assert_tag_in_string content, :tag => 'li', :attributes => { :class => 'product' }, :descendant => { :tag => 'a', :content => /product two/ }
  end

  should 'display the default minor image if thumbnails were not processed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})

    block.expects(:products).returns(enterprise.products)
    ActionView::Base.any_instance.stubs(:block_title).returns("")

    content = render_block_content(block)

    assert_tag_in_string content, :tag => 'a', :attributes => { :style => /image-loading-minor.png/ }
  end

  should 'display the thumbnail image if thumbnails were processed' do
    enterprise = create(Enterprise, :name => 'testenterprise', :identifier => 'testenterprise')
    create(Product, :enterprise => enterprise, :name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})

    process_delayed_job_queue
    block.expects(:products).returns(enterprise.products.reload)
    ActionView::Base.any_instance.stubs(:block_title).returns("")

    content = render_block_content(block)
    assert_tag_in_string content, :tag => 'a', :attributes => { :style => /rails_minor.png/ }
  end
end
{'fraction_non_alphanumeric': 0.13736020724079107, 'fraction_numerical': 0.004296455424274973, 'mean_word_length': 4.842746400885936, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 273, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6518553', 'n_tokens_mistral': 4603, 'n_tokens_neox': 4488, 'n_words': 1214}